<?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=Ppreeti</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=Ppreeti"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Ppreeti"/>
	<updated>2026-05-22T01:19:54Z</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_4c_ap&amp;diff=54015</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=54015"/>
		<updated>2011-10-21T16:47:26Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Regular Expressions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] was built as a better [http://en.wikipedia.org/wiki/Perl Perl] hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
&amp;gt;&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string1=&amp;quot;I will drill for a well for you&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well for you&amp;quot;&lt;br /&gt;
r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Character&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| []&lt;br /&gt;
| range specificication (e.g., [a-z] means a letter in the range a to z)&lt;br /&gt;
|-&lt;br /&gt;
| \w &lt;br /&gt;
| word character; same as [0-9A-Za-z_]&lt;br /&gt;
|-&lt;br /&gt;
| \W&lt;br /&gt;
| non-word character&lt;br /&gt;
|-&lt;br /&gt;
| \s&lt;br /&gt;
| space character; same as [ \t\n\r\f]&lt;br /&gt;
|-&lt;br /&gt;
| \S&lt;br /&gt;
| non-space character&lt;br /&gt;
|-&lt;br /&gt;
| \d&lt;br /&gt;
| digit character; same as [0-9]&lt;br /&gt;
|-&lt;br /&gt;
| \D&lt;br /&gt;
| non-digit character&lt;br /&gt;
|-&lt;br /&gt;
| \b&lt;br /&gt;
| backspace (0x08) (only if in a range specification)&lt;br /&gt;
|-&lt;br /&gt;
| \B&lt;br /&gt;
| non-word boundary&lt;br /&gt;
|-&lt;br /&gt;
|{m,n}	 &lt;br /&gt;
|at least m and at most n repetitions of the preceding &lt;br /&gt;
|&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++], letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in [http://en.wikipedia.org/wiki/Java_(programming_language) Java].&lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
module Persistence&lt;br /&gt;
  def load sFileName&lt;br /&gt;
   puts &amp;quot;load code to read #{sFileName} contents into my_data&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 def save sFileName&lt;br /&gt;
  puts &amp;quot;Uber code to persist #{@my_data} to #{sFileName}&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 def kind&lt;br /&gt;
  puts &amp;quot;#{self.class.name}&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BrandNewClass&lt;br /&gt;
    include Persistence&lt;br /&gt;
    attr :my_data&lt;br /&gt;
&lt;br /&gt;
        def data=(someData)&lt;br /&gt;
        @my_data = someData&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = BrandNewClass.new&lt;br /&gt;
b.data = &amp;quot;My pwd&amp;quot;&lt;br /&gt;
b.save &amp;quot;MyFile.secret&amp;quot;&lt;br /&gt;
Uber code to persist My pwd to MyFile.secret&lt;br /&gt;
&lt;br /&gt;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
The comparable mixin is used by classes whose objects may be ordered.The 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. if the other object is not comparable then is should just return nil.Comparable uses &amp;lt;=&amp;gt; to implement the comparison operators(&amp;lt;,&amp;lt;=,==,=&amp;gt;,and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
Here is an example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate comparable mixin.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line &lt;br /&gt;
  def initialize(x1, y1, x2, y2) &lt;br /&gt;
    @x1, @y1, @x2, @y2 = x1, y1, x2, y2 &lt;br /&gt;
  end   &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We compare two lines on the basis of their lengths.&lt;br /&gt;
We add the Comparable mixin as follows: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line  &lt;br /&gt;
  include Comparable  &lt;br /&gt;
  def length_squared &lt;br /&gt;
    (@x2-@x1) * (@x2-@x1) + (@y2-@y1) * (@y2-@y1)&lt;br /&gt;
end&lt;br /&gt;
def &amp;lt;=&amp;gt;(other) &lt;br /&gt;
    self.length_squared &amp;lt;=&amp;gt; other.length_squared &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;=&amp;gt; returns 1,0, or –1, depending on  whether the first argument is &lt;br /&gt;
greater than, equal to, or less than the second argument.  &lt;br /&gt;
We delegate the call to &amp;lt;=&amp;gt; of the Fixnum class, which compares &lt;br /&gt;
the squares of the lengths. &lt;br /&gt;
Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
l1 = Line.new(1, 0, 4, 3) &lt;br /&gt;
l2 = Line.new(0, 0, 10, 10) &lt;br /&gt;
puts l1.length_squared &lt;br /&gt;
if l1 &amp;lt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is shorter than Line 2&amp;quot; &lt;br /&gt;
else if l1 &amp;gt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is longer than Line 2&amp;quot; &lt;br /&gt;
else &lt;br /&gt;
  puts &amp;quot;Line 1 is just as long as Line 2&amp;quot; &lt;br /&gt;
 end &lt;br /&gt;
end &lt;br /&gt;
&amp;gt;&amp;gt;Line 1 is shorter than Line 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
Enumerable is a standard mixin which can be used to put into other classes.It has a method called inject that can be applied to adjacent elements of a set.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[1, 2, 3, 4, 5 ,6].inject {|v, n| v+n }&lt;br /&gt;
&amp;gt;&amp;gt;21&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen in the above example inject is used on all elements of the set to add them and display the result.&lt;br /&gt;
&lt;br /&gt;
To understand the working of Enumerable mixin in class,consider the following example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class VowelFinder &lt;br /&gt;
  include Enumerable &lt;br /&gt;
  def initialize(string) &lt;br /&gt;
    @string = string &lt;br /&gt;
  end &lt;br /&gt;
  def each &lt;br /&gt;
    @string.scan(/[aeiou]/) do |vowel| &lt;br /&gt;
      yield vowel &lt;br /&gt;
    end &lt;br /&gt;
  end &lt;br /&gt;
end  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
VowelFinder.new(&amp;quot;abacadabra&amp;quot;).inject {|v, n| v+n} &lt;br /&gt;
&amp;gt;&amp;gt;aaaaa&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] has several disadvantages that can lead to ambiguous code behavior either during [http://en.wikipedia.org/wiki/Compile_time compile time] or [http://en.wikipedia.org/wiki/Run_time run time]. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules]. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
      puts &amp;quot;Inside a1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
      puts &amp;quot;Inside a2&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
       puts &amp;quot;Inside b1&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
       puts &amp;quot;Inside b2&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
      puts &amp;quot;Inside s1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1             # =&amp;gt; Inside a1&lt;br /&gt;
  obj.a2             # =&amp;gt; Inside a2&lt;br /&gt;
  obj.b1             # =&amp;gt; Inside b1&lt;br /&gt;
  obj.b2             # =&amp;gt; Inside b2&lt;br /&gt;
  obj.s1             # =&amp;gt; Inside s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses super-classes].&amp;lt;br&amp;gt;&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super-classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses sub-class].&amp;lt;br&amp;gt; &lt;br /&gt;
For example, [http://en.wikipedia.org/wiki/Mixin Mixins] help achieve this.&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by [http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html implementing the interfaces]. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase [http://en.wikipedia.org/wiki/Reusability reusability] and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
Some people call this situation the '''&amp;quot;[http://en.wikipedia.org/wiki/Diamond_problem Deadly Diamond of Death]&amp;quot;'''. This is illustrated by the Java code example below:&lt;br /&gt;
&lt;br /&gt;
  // This is our top most abstract class.&lt;br /&gt;
  class AbstractSuperClass{&lt;br /&gt;
    abstract void method();&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // These are the two concrete sub classes which extend the above super class&lt;br /&gt;
  class ConcreteSubOne extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I am going to test multiple Inheritance&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  class ConcreteSubTwo extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I will cause the Deadly Diamond of Death&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  // This is our last class which extends both of the above concrete classes&lt;br /&gt;
  class DeadlyDiamondEffect extends ConcreteSubOne, ConcreteSubTwo{&lt;br /&gt;
    //Some methods of this class&lt;br /&gt;
    //This inherits a do() method from ConcreteSubOne and another do() from ConcreteSubTwo. When this is called there is an ambiguity.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a [http://en.wikipedia.org/wiki/Object_composition object composition] but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;[http://en.wikipedia.org/wiki/Is-a Is-a]&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming renaming].&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://strugglingwithruby.blogspot.com/2009/05/regular-expressions-in-ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/regexp.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/modules_mixins.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html ]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/355633/what-are-some-good-examples-of-mixins-and-or-traits] &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://javacodeonline.blogspot.com/2009/08/deadly-diamond-of-death.html Deadly Diamond of Death]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem&lt;br /&gt;
[http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses Subclasses and Superclasses]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&amp;lt;br&amp;gt;&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &amp;lt;br&amp;gt;&lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.java2s.com/Code/Ruby/Class/Rubytermsmultipleinheritanceamixin Ruby Multiple Inheritance with modules and mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &amp;lt;br&amp;gt;&lt;br /&gt;
http://rubyonrails.org/ &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53339</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53339"/>
		<updated>2011-10-20T23:11:03Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] was built as a better [http://en.wikipedia.org/wiki/Perl Perl] hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
&amp;gt;&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Character&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| []&lt;br /&gt;
| range specificication (e.g., [a-z] means a letter in the range a to z)&lt;br /&gt;
|-&lt;br /&gt;
| \w &lt;br /&gt;
| word character; same as [0-9A-Za-z_]&lt;br /&gt;
|-&lt;br /&gt;
| \W&lt;br /&gt;
| non-word character&lt;br /&gt;
|-&lt;br /&gt;
| \s&lt;br /&gt;
| space character; same as [ \t\n\r\f]&lt;br /&gt;
|-&lt;br /&gt;
| \S&lt;br /&gt;
| non-space character&lt;br /&gt;
|-&lt;br /&gt;
| \d&lt;br /&gt;
| digit character; same as [0-9]&lt;br /&gt;
|-&lt;br /&gt;
| \D&lt;br /&gt;
| non-digit character&lt;br /&gt;
|-&lt;br /&gt;
| \b&lt;br /&gt;
| backspace (0x08) (only if in a range specification)&lt;br /&gt;
|-&lt;br /&gt;
| \B&lt;br /&gt;
| non-word boundary&lt;br /&gt;
|-&lt;br /&gt;
|{m,n}	 &lt;br /&gt;
|at least m and at most n repetitions of the preceding &lt;br /&gt;
|&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++], letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in [http://en.wikipedia.org/wiki/Java_(programming_language) Java].&lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
module Persistence&lt;br /&gt;
  def load sFileName&lt;br /&gt;
   puts &amp;quot;load code to read #{sFileName} contents into my_data&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 def save sFileName&lt;br /&gt;
  puts &amp;quot;Uber code to persist #{@my_data} to #{sFileName}&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 def kind&lt;br /&gt;
  puts &amp;quot;#{self.class.name}&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BrandNewClass&lt;br /&gt;
    include Persistence&lt;br /&gt;
    attr :my_data&lt;br /&gt;
&lt;br /&gt;
        def data=(someData)&lt;br /&gt;
        @my_data = someData&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = BrandNewClass.new&lt;br /&gt;
b.data = &amp;quot;My pwd&amp;quot;&lt;br /&gt;
b.save &amp;quot;MyFile.secret&amp;quot;&lt;br /&gt;
Uber code to persist My pwd to MyFile.secret&lt;br /&gt;
&lt;br /&gt;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
The comparable mixin is used by classes whose objects may be ordered.The 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. if the other object is not comparable then is should just return nil.Comparable uses &amp;lt;=&amp;gt; to implement the comparison operators(&amp;lt;,&amp;lt;=,==,=&amp;gt;,and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
Here is an example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate comparable mixin.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line &lt;br /&gt;
  def initialize(x1, y1, x2, y2) &lt;br /&gt;
    @x1, @y1, @x2, @y2 = x1, y1, x2, y2 &lt;br /&gt;
  end   &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We compare two lines on the basis of their lengths.&lt;br /&gt;
We add the Comparable mixin as follows: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line  &lt;br /&gt;
  include Comparable  &lt;br /&gt;
  def length_squared &lt;br /&gt;
    (@x2-@x1) * (@x2-@x1) + (@y2-@y1) * (@y2-@y1)&lt;br /&gt;
end&lt;br /&gt;
def &amp;lt;=&amp;gt;(other) &lt;br /&gt;
    self.length_squared &amp;lt;=&amp;gt; other.length_squared &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;=&amp;gt; returns 1,0, or –1, depending on  whether the first argument is &lt;br /&gt;
greater than, equal to, or less than the second argument.  &lt;br /&gt;
We delegate the call to &amp;lt;=&amp;gt; of the Fixnum class, which compares &lt;br /&gt;
the squares of the lengths. &lt;br /&gt;
Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
l1 = Line.new(1, 0, 4, 3) &lt;br /&gt;
l2 = Line.new(0, 0, 10, 10) &lt;br /&gt;
puts l1.length_squared &lt;br /&gt;
if l1 &amp;lt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is shorter than Line 2&amp;quot; &lt;br /&gt;
else if l1 &amp;gt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is longer than Line 2&amp;quot; &lt;br /&gt;
else &lt;br /&gt;
  puts &amp;quot;Line 1 is just as long as Line 2&amp;quot; &lt;br /&gt;
 end &lt;br /&gt;
end &lt;br /&gt;
&amp;gt;&amp;gt;Line 1 is shorter than Line 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
Enumerable is a standard mixin which can be used to put into other classes.It has a method called inject that can be applied to adjacent elements of a set.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[1, 2, 3, 4, 5 ,6].inject {|v, n| v+n }&lt;br /&gt;
&amp;gt;&amp;gt;21&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen in the above example inject is used on all elements of the set to add them and display the result.&lt;br /&gt;
&lt;br /&gt;
To understand the working of Enumerable mixin in class,consider the following example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class VowelFinder &lt;br /&gt;
  include Enumerable &lt;br /&gt;
  def initialize(string) &lt;br /&gt;
    @string = string &lt;br /&gt;
  end &lt;br /&gt;
  def each &lt;br /&gt;
    @string.scan(/[aeiou]/) do |vowel| &lt;br /&gt;
      yield vowel &lt;br /&gt;
    end &lt;br /&gt;
  end &lt;br /&gt;
end  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
VowelFinder.new(&amp;quot;abacadabra&amp;quot;).inject {|v, n| v+n} &lt;br /&gt;
&amp;gt;&amp;gt;aaaaa&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] has several disadvantages that can lead to ambiguous code behavior either during [http://en.wikipedia.org/wiki/Compile_time compile time] or [http://en.wikipedia.org/wiki/Run_time run time]. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules]. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
      puts &amp;quot;Inside a1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
      puts &amp;quot;Inside a2&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
       puts &amp;quot;Inside b1&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
       puts &amp;quot;Inside b2&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
      puts &amp;quot;Inside s1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1             # =&amp;gt; Inside a1&lt;br /&gt;
  obj.a2             # =&amp;gt; Inside a2&lt;br /&gt;
  obj.b1             # =&amp;gt; Inside b1&lt;br /&gt;
  obj.b2             # =&amp;gt; Inside b2&lt;br /&gt;
  obj.s1             # =&amp;gt; Inside s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses super-classes].&amp;lt;br&amp;gt;&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super-classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses sub-class].&amp;lt;br&amp;gt; &lt;br /&gt;
For example, [http://en.wikipedia.org/wiki/Mixin Mixins] help achieve this.&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by [http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html implementing the interfaces]. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase [http://en.wikipedia.org/wiki/Reusability reusability] and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
Some people call this situation the '''&amp;quot;[http://en.wikipedia.org/wiki/Diamond_problem Deadly Diamond of Death]&amp;quot;'''. This is illustrated by the Java code example below:&lt;br /&gt;
&lt;br /&gt;
  // This is our top most abstract class.&lt;br /&gt;
  class AbstractSuperClass{&lt;br /&gt;
    abstract void method();&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // These are the two concrete sub classes which extend the above super class&lt;br /&gt;
  class ConcreteSubOne extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I am going to test multiple Inheritance&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  class ConcreteSubTwo extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I will cause the Deadly Diamond of Death&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  // This is our last class which extends both of the above concrete classes&lt;br /&gt;
  class DeadlyDiamondEffect extends ConcreteSubOne, ConcreteSubTwo{&lt;br /&gt;
    //Some methods of this class&lt;br /&gt;
    //This inherits a do() method from ConcreteSubOne and another do() from ConcreteSubTwo. When this is called there is an ambiguity.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a [http://en.wikipedia.org/wiki/Object_composition object composition] but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;[http://en.wikipedia.org/wiki/Is-a Is-a]&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming renaming].&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://strugglingwithruby.blogspot.com/2009/05/regular-expressions-in-ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/regexp.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/modules_mixins.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html ]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://stackoverflow.com/questions/355633/what-are-some-good-examples-of-mixins-and-or-traits] &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://javacodeonline.blogspot.com/2009/08/deadly-diamond-of-death.html Deadly Diamond of Death]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem&lt;br /&gt;
[http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses Subclasses and Superclasses]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&amp;lt;br&amp;gt;&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &amp;lt;br&amp;gt;&lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.java2s.com/Code/Ruby/Class/Rubytermsmultipleinheritanceamixin Ruby Multiple Inheritance with modules and mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &amp;lt;br&amp;gt;&lt;br /&gt;
http://rubyonrails.org/ &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53332</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53332"/>
		<updated>2011-10-20T23:09:02Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] was built as a better [http://en.wikipedia.org/wiki/Perl Perl] hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
&amp;gt;&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Character&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| []&lt;br /&gt;
| range specificication (e.g., [a-z] means a letter in the range a to z)&lt;br /&gt;
|-&lt;br /&gt;
| \w &lt;br /&gt;
| word character; same as [0-9A-Za-z_]&lt;br /&gt;
|-&lt;br /&gt;
| \W&lt;br /&gt;
| non-word character&lt;br /&gt;
|-&lt;br /&gt;
| \s&lt;br /&gt;
| space character; same as [ \t\n\r\f]&lt;br /&gt;
|-&lt;br /&gt;
| \S&lt;br /&gt;
| non-space character&lt;br /&gt;
|-&lt;br /&gt;
| \d&lt;br /&gt;
| digit character; same as [0-9]&lt;br /&gt;
|-&lt;br /&gt;
| \D&lt;br /&gt;
| non-digit character&lt;br /&gt;
|-&lt;br /&gt;
| \b&lt;br /&gt;
| backspace (0x08) (only if in a range specification)&lt;br /&gt;
|-&lt;br /&gt;
| \B&lt;br /&gt;
| non-word boundary&lt;br /&gt;
|-&lt;br /&gt;
|{m,n}	 &lt;br /&gt;
|at least m and at most n repetitions of the preceding &lt;br /&gt;
|&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++], letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in [http://en.wikipedia.org/wiki/Java_(programming_language) Java].&lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
module Persistence&lt;br /&gt;
  def load sFileName&lt;br /&gt;
   puts &amp;quot;load code to read #{sFileName} contents into my_data&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 def save sFileName&lt;br /&gt;
  puts &amp;quot;Uber code to persist #{@my_data} to #{sFileName}&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 def kind&lt;br /&gt;
  puts &amp;quot;#{self.class.name}&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BrandNewClass&lt;br /&gt;
    include Persistence&lt;br /&gt;
    attr :my_data&lt;br /&gt;
&lt;br /&gt;
        def data=(someData)&lt;br /&gt;
        @my_data = someData&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = BrandNewClass.new&lt;br /&gt;
b.data = &amp;quot;My pwd&amp;quot;&lt;br /&gt;
b.save &amp;quot;MyFile.secret&amp;quot;&lt;br /&gt;
Uber code to persist My pwd to MyFile.secret&lt;br /&gt;
&lt;br /&gt;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
The comparable mixin is used by classes whose objects may be ordered.The 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. if the other object is not comparable then is should just return nil.Comparable uses &amp;lt;=&amp;gt; to implement the comparison operators(&amp;lt;,&amp;lt;=,==,=&amp;gt;,and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
Here is an example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate comparable mixin.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line &lt;br /&gt;
  def initialize(x1, y1, x2, y2) &lt;br /&gt;
    @x1, @y1, @x2, @y2 = x1, y1, x2, y2 &lt;br /&gt;
  end   &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We compare two lines on the basis of their lengths.&lt;br /&gt;
We add the Comparable mixin as follows: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line  &lt;br /&gt;
  include Comparable  &lt;br /&gt;
  def length_squared &lt;br /&gt;
    (@x2-@x1) * (@x2-@x1) + (@y2-@y1) * (@y2-@y1)&lt;br /&gt;
end&lt;br /&gt;
def &amp;lt;=&amp;gt;(other) &lt;br /&gt;
    self.length_squared &amp;lt;=&amp;gt; other.length_squared &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;=&amp;gt; returns 1,0, or –1, depending on  whether the first argument is &lt;br /&gt;
greater than, equal to, or less than the second argument.  &lt;br /&gt;
We delegate the call to &amp;lt;=&amp;gt; of the Fixnum class, which compares &lt;br /&gt;
the squares of the lengths. &lt;br /&gt;
Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
l1 = Line.new(1, 0, 4, 3) &lt;br /&gt;
l2 = Line.new(0, 0, 10, 10) &lt;br /&gt;
puts l1.length_squared &lt;br /&gt;
if l1 &amp;lt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is shorter than Line 2&amp;quot; &lt;br /&gt;
else if l1 &amp;gt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is longer than Line 2&amp;quot; &lt;br /&gt;
else &lt;br /&gt;
  puts &amp;quot;Line 1 is just as long as Line 2&amp;quot; &lt;br /&gt;
 end &lt;br /&gt;
end &lt;br /&gt;
&amp;gt;&amp;gt;Line 1 is shorter than Line 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
Enumerable is a standard mixin which can be used to put into other classes.It has a method called inject that can be applied to adjacent elements of a set.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[1, 2, 3, 4, 5 ,6].inject {|v, n| v+n }&lt;br /&gt;
&amp;gt;&amp;gt;21&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen in the above example inject is used on all elements of the set to add them and display the result.&lt;br /&gt;
&lt;br /&gt;
To understand the working of Enumerable mixin in class,consider the following example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class VowelFinder &lt;br /&gt;
  include Enumerable &lt;br /&gt;
  def initialize(string) &lt;br /&gt;
    @string = string &lt;br /&gt;
  end &lt;br /&gt;
  def each &lt;br /&gt;
    @string.scan(/[aeiou]/) do |vowel| &lt;br /&gt;
      yield vowel &lt;br /&gt;
    end &lt;br /&gt;
  end &lt;br /&gt;
end  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
VowelFinder.new(&amp;quot;abacadabra&amp;quot;).inject {|v, n| v+n} &lt;br /&gt;
&amp;gt;&amp;gt;aaaaa&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] has several disadvantages that can lead to ambiguous code behavior either during [http://en.wikipedia.org/wiki/Compile_time compile time] or [http://en.wikipedia.org/wiki/Run_time run time]. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules]. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
      puts &amp;quot;Inside a1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
      puts &amp;quot;Inside a2&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
       puts &amp;quot;Inside b1&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
       puts &amp;quot;Inside b2&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
      puts &amp;quot;Inside s1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1             # =&amp;gt; Inside a1&lt;br /&gt;
  obj.a2             # =&amp;gt; Inside a2&lt;br /&gt;
  obj.b1             # =&amp;gt; Inside b1&lt;br /&gt;
  obj.b2             # =&amp;gt; Inside b2&lt;br /&gt;
  obj.s1             # =&amp;gt; Inside s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses super-classes].&amp;lt;br&amp;gt;&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super-classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses sub-class].&amp;lt;br&amp;gt; &lt;br /&gt;
For example, [http://en.wikipedia.org/wiki/Mixin Mixins] help achieve this.&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by [http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html implementing the interfaces]. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase [http://en.wikipedia.org/wiki/Reusability reusability] and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
Some people call this situation the '''&amp;quot;[http://en.wikipedia.org/wiki/Diamond_problem Deadly Diamond of Death]&amp;quot;'''. This is illustrated by the Java code example below:&lt;br /&gt;
&lt;br /&gt;
  // This is our top most abstract class.&lt;br /&gt;
  class AbstractSuperClass{&lt;br /&gt;
    abstract void method();&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // These are the two concrete sub classes which extend the above super class&lt;br /&gt;
  class ConcreteSubOne extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I am going to test multiple Inheritance&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  class ConcreteSubTwo extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I will cause the Deadly Diamond of Death&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  // This is our last class which extends both of the above concrete classes&lt;br /&gt;
  class DeadlyDiamondEffect extends ConcreteSubOne, ConcreteSubTwo{&lt;br /&gt;
    //Some methods of this class&lt;br /&gt;
    //This inherits a do() method from ConcreteSubOne and another do() from ConcreteSubTwo. When this is called there is an ambiguity.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a [http://en.wikipedia.org/wiki/Object_composition object composition] but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;[http://en.wikipedia.org/wiki/Is-a Is-a]&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming renaming].&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://strugglingwithruby.blogspot.com/2009/05/regular-expressions-in-ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/regexp.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/modules_mixins.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://javacodeonline.blogspot.com/2009/08/deadly-diamond-of-death.html Deadly Diamond of Death]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem&lt;br /&gt;
[http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses Subclasses and Superclasses]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&amp;lt;br&amp;gt;&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &amp;lt;br&amp;gt;&lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.java2s.com/Code/Ruby/Class/Rubytermsmultipleinheritanceamixin Ruby Multiple Inheritance with modules and mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &amp;lt;br&amp;gt;&lt;br /&gt;
http://rubyonrails.org/ &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53330</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53330"/>
		<updated>2011-10-20T23:08:10Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] was built as a better [http://en.wikipedia.org/wiki/Perl Perl] hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
&amp;gt;&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Character&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| []&lt;br /&gt;
| range specificication (e.g., [a-z] means a letter in the range a to z)&lt;br /&gt;
|-&lt;br /&gt;
| \w &lt;br /&gt;
| word character; same as [0-9A-Za-z_]&lt;br /&gt;
|-&lt;br /&gt;
| \W&lt;br /&gt;
| non-word character&lt;br /&gt;
|-&lt;br /&gt;
| \s&lt;br /&gt;
| space character; same as [ \t\n\r\f]&lt;br /&gt;
|-&lt;br /&gt;
| \S&lt;br /&gt;
| non-space character&lt;br /&gt;
|-&lt;br /&gt;
| \d&lt;br /&gt;
| digit character; same as [0-9]&lt;br /&gt;
|-&lt;br /&gt;
| \D&lt;br /&gt;
| non-digit character&lt;br /&gt;
|-&lt;br /&gt;
| \b&lt;br /&gt;
| backspace (0x08) (only if in a range specification)&lt;br /&gt;
|-&lt;br /&gt;
| \B&lt;br /&gt;
| non-word boundary&lt;br /&gt;
|-&lt;br /&gt;
|{m,n}	 &lt;br /&gt;
|at least m and at most n repetitions of the preceding &lt;br /&gt;
|&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++], letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in [http://en.wikipedia.org/wiki/Java_(programming_language) Java].&lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
module Persistence&lt;br /&gt;
  def load sFileName&lt;br /&gt;
   puts &amp;quot;load code to read #{sFileName} contents into my_data&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 def save sFileName&lt;br /&gt;
  puts &amp;quot;Uber code to persist #{@my_data} to #{sFileName}&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 def kind&lt;br /&gt;
  puts &amp;quot;#{self.class.name}&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class BrandNewClass&lt;br /&gt;
    include Persistence&lt;br /&gt;
    attr :my_data&lt;br /&gt;
&lt;br /&gt;
        def data=(someData)&lt;br /&gt;
        @my_data = someData&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = BrandNewClass.new&lt;br /&gt;
b.data = &amp;quot;My pwd&amp;quot;&lt;br /&gt;
b.save &amp;quot;MyFile.secret&amp;quot;&lt;br /&gt;
Uber code to persist My pwd to MyFile.secret&lt;br /&gt;
&amp;gt;&amp;gt;b = BrandNewClass.new&lt;br /&gt;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
The comparable mixin is used by classes whose objects may be ordered.The 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. if the other object is not comparable then is should just return nil.Comparable uses &amp;lt;=&amp;gt; to implement the comparison operators(&amp;lt;,&amp;lt;=,==,=&amp;gt;,and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
Here is an example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate comparable mixin.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line &lt;br /&gt;
  def initialize(x1, y1, x2, y2) &lt;br /&gt;
    @x1, @y1, @x2, @y2 = x1, y1, x2, y2 &lt;br /&gt;
  end   &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We compare two lines on the basis of their lengths.&lt;br /&gt;
We add the Comparable mixin as follows: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line  &lt;br /&gt;
  include Comparable  &lt;br /&gt;
  def length_squared &lt;br /&gt;
    (@x2-@x1) * (@x2-@x1) + (@y2-@y1) * (@y2-@y1)&lt;br /&gt;
end&lt;br /&gt;
def &amp;lt;=&amp;gt;(other) &lt;br /&gt;
    self.length_squared &amp;lt;=&amp;gt; other.length_squared &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;=&amp;gt; returns 1,0, or –1, depending on  whether the first argument is &lt;br /&gt;
greater than, equal to, or less than the second argument.  &lt;br /&gt;
We delegate the call to &amp;lt;=&amp;gt; of the Fixnum class, which compares &lt;br /&gt;
the squares of the lengths. &lt;br /&gt;
Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
l1 = Line.new(1, 0, 4, 3) &lt;br /&gt;
l2 = Line.new(0, 0, 10, 10) &lt;br /&gt;
puts l1.length_squared &lt;br /&gt;
if l1 &amp;lt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is shorter than Line 2&amp;quot; &lt;br /&gt;
else if l1 &amp;gt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is longer than Line 2&amp;quot; &lt;br /&gt;
else &lt;br /&gt;
  puts &amp;quot;Line 1 is just as long as Line 2&amp;quot; &lt;br /&gt;
 end &lt;br /&gt;
end &lt;br /&gt;
&amp;gt;&amp;gt;Line 1 is shorter than Line 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
Enumerable is a standard mixin which can be used to put into other classes.It has a method called inject that can be applied to adjacent elements of a set.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[1, 2, 3, 4, 5 ,6].inject {|v, n| v+n }&lt;br /&gt;
&amp;gt;&amp;gt;21&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen in the above example inject is used on all elements of the set to add them and display the result.&lt;br /&gt;
&lt;br /&gt;
To understand the working of Enumerable mixin in class,consider the following example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class VowelFinder &lt;br /&gt;
  include Enumerable &lt;br /&gt;
  def initialize(string) &lt;br /&gt;
    @string = string &lt;br /&gt;
  end &lt;br /&gt;
  def each &lt;br /&gt;
    @string.scan(/[aeiou]/) do |vowel| &lt;br /&gt;
      yield vowel &lt;br /&gt;
    end &lt;br /&gt;
  end &lt;br /&gt;
end  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
VowelFinder.new(&amp;quot;abacadabra&amp;quot;).inject {|v, n| v+n} &lt;br /&gt;
&amp;gt;&amp;gt;aaaaa&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] has several disadvantages that can lead to ambiguous code behavior either during [http://en.wikipedia.org/wiki/Compile_time compile time] or [http://en.wikipedia.org/wiki/Run_time run time]. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules]. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
      puts &amp;quot;Inside a1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
      puts &amp;quot;Inside a2&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
       puts &amp;quot;Inside b1&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
       puts &amp;quot;Inside b2&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
      puts &amp;quot;Inside s1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1             # =&amp;gt; Inside a1&lt;br /&gt;
  obj.a2             # =&amp;gt; Inside a2&lt;br /&gt;
  obj.b1             # =&amp;gt; Inside b1&lt;br /&gt;
  obj.b2             # =&amp;gt; Inside b2&lt;br /&gt;
  obj.s1             # =&amp;gt; Inside s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses super-classes].&amp;lt;br&amp;gt;&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super-classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses sub-class].&amp;lt;br&amp;gt; &lt;br /&gt;
For example, [http://en.wikipedia.org/wiki/Mixin Mixins] help achieve this.&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by [http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html implementing the interfaces]. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase [http://en.wikipedia.org/wiki/Reusability reusability] and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
Some people call this situation the '''&amp;quot;[http://en.wikipedia.org/wiki/Diamond_problem Deadly Diamond of Death]&amp;quot;'''. This is illustrated by the Java code example below:&lt;br /&gt;
&lt;br /&gt;
  // This is our top most abstract class.&lt;br /&gt;
  class AbstractSuperClass{&lt;br /&gt;
    abstract void method();&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // These are the two concrete sub classes which extend the above super class&lt;br /&gt;
  class ConcreteSubOne extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I am going to test multiple Inheritance&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  class ConcreteSubTwo extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I will cause the Deadly Diamond of Death&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  // This is our last class which extends both of the above concrete classes&lt;br /&gt;
  class DeadlyDiamondEffect extends ConcreteSubOne, ConcreteSubTwo{&lt;br /&gt;
    //Some methods of this class&lt;br /&gt;
    //This inherits a do() method from ConcreteSubOne and another do() from ConcreteSubTwo. When this is called there is an ambiguity.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a [http://en.wikipedia.org/wiki/Object_composition object composition] but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;[http://en.wikipedia.org/wiki/Is-a Is-a]&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming renaming].&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://strugglingwithruby.blogspot.com/2009/05/regular-expressions-in-ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/regexp.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/modules_mixins.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://javacodeonline.blogspot.com/2009/08/deadly-diamond-of-death.html Deadly Diamond of Death]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem&lt;br /&gt;
[http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses Subclasses and Superclasses]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&amp;lt;br&amp;gt;&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &amp;lt;br&amp;gt;&lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.java2s.com/Code/Ruby/Class/Rubytermsmultipleinheritanceamixin Ruby Multiple Inheritance with modules and mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &amp;lt;br&amp;gt;&lt;br /&gt;
http://rubyonrails.org/ &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53084</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53084"/>
		<updated>2011-10-20T16:39:28Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Composing Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] was built as a better [http://en.wikipedia.org/wiki/Perl Perl] hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
&amp;gt;&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Character&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| []&lt;br /&gt;
| range specificication (e.g., [a-z] means a letter in the range a to z)&lt;br /&gt;
|-&lt;br /&gt;
| \w &lt;br /&gt;
| word character; same as [0-9A-Za-z_]&lt;br /&gt;
|-&lt;br /&gt;
| \W&lt;br /&gt;
| non-word character&lt;br /&gt;
|-&lt;br /&gt;
| \s&lt;br /&gt;
| space character; same as [ \t\n\r\f]&lt;br /&gt;
|-&lt;br /&gt;
| \S&lt;br /&gt;
| non-space character&lt;br /&gt;
|-&lt;br /&gt;
| \d&lt;br /&gt;
| digit character; same as [0-9]&lt;br /&gt;
|-&lt;br /&gt;
| \D&lt;br /&gt;
| non-digit character&lt;br /&gt;
|-&lt;br /&gt;
| \b&lt;br /&gt;
| backspace (0x08) (only if in a range specification)&lt;br /&gt;
|-&lt;br /&gt;
| \B&lt;br /&gt;
| non-word boundary&lt;br /&gt;
|-&lt;br /&gt;
|{m,n}	 &lt;br /&gt;
|at least m and at most n repetitions of the preceding &lt;br /&gt;
|&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++], letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in [http://en.wikipedia.org/wiki/Java_(programming_language) Java].&lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
The comparable mixin is used by classes whose objects may be ordered.The 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. if the other object is not comparable then is should just return nil.Comparable uses &amp;lt;=&amp;gt; to implement the comparison operators(&amp;lt;,&amp;lt;=,==,=&amp;gt;,and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
Here is an example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate comparable mixin.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line &lt;br /&gt;
  def initialize(x1, y1, x2, y2) &lt;br /&gt;
    @x1, @y1, @x2, @y2 = x1, y1, x2, y2 &lt;br /&gt;
  end   &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We compare two lines on the basis of their lengths.&lt;br /&gt;
We add the Comparable mixin as follows: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line  &lt;br /&gt;
  include Comparable  &lt;br /&gt;
  def length_squared &lt;br /&gt;
    (@x2-@x1) * (@x2-@x1) + (@y2-@y1) * (@y2-@y1)&lt;br /&gt;
end&lt;br /&gt;
def &amp;lt;=&amp;gt;(other) &lt;br /&gt;
    self.length_squared &amp;lt;=&amp;gt; other.length_squared &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;=&amp;gt; returns 1,0, or –1, depending on  whether the first argument is &lt;br /&gt;
greater than, equal to, or less than the second argument.  &lt;br /&gt;
We delegate the call to &amp;lt;=&amp;gt; of the Fixnum class, which compares &lt;br /&gt;
the squares of the lengths. &lt;br /&gt;
Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
l1 = Line.new(1, 0, 4, 3) &lt;br /&gt;
l2 = Line.new(0, 0, 10, 10) &lt;br /&gt;
puts l1.length_squared &lt;br /&gt;
if l1 &amp;lt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is shorter than Line 2&amp;quot; &lt;br /&gt;
else if l1 &amp;gt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is longer than Line 2&amp;quot; &lt;br /&gt;
else &lt;br /&gt;
  puts &amp;quot;Line 1 is just as long as Line 2&amp;quot; &lt;br /&gt;
 end &lt;br /&gt;
end &lt;br /&gt;
&amp;gt;&amp;gt;Line 1 is shorter than Line 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
Enumerable is a standard mixin which can be used to put into other classes.It has a method called inject that can be applied to adjacent elements of a set.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[1, 2, 3, 4, 5 ,6].inject {|v, n| v+n }&lt;br /&gt;
&amp;gt;&amp;gt;21&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen in the above example inject is used on all elements of the set to add them and display the result.&lt;br /&gt;
&lt;br /&gt;
To understand the working of Enumerable mixin in class,consider the following example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class VowelFinder &lt;br /&gt;
  include Enumerable &lt;br /&gt;
  def initialize(string) &lt;br /&gt;
    @string = string &lt;br /&gt;
  end &lt;br /&gt;
  def each &lt;br /&gt;
    @string.scan(/[aeiou]/) do |vowel| &lt;br /&gt;
      yield vowel &lt;br /&gt;
    end &lt;br /&gt;
  end &lt;br /&gt;
end  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
VowelFinder.new(&amp;quot;abacadabra&amp;quot;).inject {|v, n| v+n} &lt;br /&gt;
&amp;gt;&amp;gt;aaaaa&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] has several disadvantages that can lead to ambiguous code behavior either during [http://en.wikipedia.org/wiki/Compile_time compile time] or [http://en.wikipedia.org/wiki/Run_time run time]. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules]. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
      puts &amp;quot;Inside a1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
      puts &amp;quot;Inside a2&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
       puts &amp;quot;Inside b1&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
       puts &amp;quot;Inside b2&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
      puts &amp;quot;Inside s1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1             # =&amp;gt; Inside a1&lt;br /&gt;
  obj.a2             # =&amp;gt; Inside a2&lt;br /&gt;
  obj.b1             # =&amp;gt; Inside b1&lt;br /&gt;
  obj.b2             # =&amp;gt; Inside b2&lt;br /&gt;
  obj.s1             # =&amp;gt; Inside s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses super-classes].&amp;lt;br&amp;gt;&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super-classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses sub-class].&amp;lt;br&amp;gt; &lt;br /&gt;
For example, [http://en.wikipedia.org/wiki/Mixin Mixins] help achieve this.&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by [http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html implementing the interfaces]. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase [http://en.wikipedia.org/wiki/Reusability reusability] and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
Some people call this situation the '''&amp;quot;[http://en.wikipedia.org/wiki/Diamond_problem Deadly Diamond of Death]&amp;quot;'''. This is illustrated by the Java code example below:&lt;br /&gt;
&lt;br /&gt;
  // This is our top most abstract class.&lt;br /&gt;
  class AbstractSuperClass{&lt;br /&gt;
    abstract void method();&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // These are the two concrete sub classes which extend the above super class&lt;br /&gt;
  class ConcreteSubOne extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I am going to test multiple Inheritance&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  class ConcreteSubTwo extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I will cause the Deadly Diamond of Death&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  // This is our last class which extends both of the above concrete classes&lt;br /&gt;
  class DeadlyDiamondEffect extends ConcreteSubOne, ConcreteSubTwo{&lt;br /&gt;
    //Some methods of this class&lt;br /&gt;
    //This inherits a do() method from ConcreteSubOne and another do() from ConcreteSubTwo. When this is called there is an ambiguity.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a [http://en.wikipedia.org/wiki/Object_composition object composition] but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;[http://en.wikipedia.org/wiki/Is-a Is-a]&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming renaming].&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://strugglingwithruby.blogspot.com/2009/05/regular-expressions-in-ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/regexp.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/modules_mixins.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://javacodeonline.blogspot.com/2009/08/deadly-diamond-of-death.html Deadly Diamond of Death]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem&lt;br /&gt;
[http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses Subclasses and Superclasses]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&amp;lt;br&amp;gt;&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &amp;lt;br&amp;gt;&lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.java2s.com/Code/Ruby/Class/Rubytermsmultipleinheritanceamixin Ruby Multiple Inheritance with modules and mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &amp;lt;br&amp;gt;&lt;br /&gt;
http://rubyonrails.org/ &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53083</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53083"/>
		<updated>2011-10-20T16:38:54Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Composing Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] was built as a better [http://en.wikipedia.org/wiki/Perl Perl] hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
&amp;gt;&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Character&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| []&lt;br /&gt;
| range specificication (e.g., [a-z] means a letter in the range a to z)&lt;br /&gt;
|-&lt;br /&gt;
| \w &lt;br /&gt;
| word character; same as [0-9A-Za-z_]&lt;br /&gt;
|-&lt;br /&gt;
| \W&lt;br /&gt;
| non-word character&lt;br /&gt;
|-&lt;br /&gt;
| \s&lt;br /&gt;
| space character; same as [ \t\n\r\f]&lt;br /&gt;
|-&lt;br /&gt;
| \S&lt;br /&gt;
| non-space character&lt;br /&gt;
|-&lt;br /&gt;
| \d&lt;br /&gt;
| digit character; same as [0-9]&lt;br /&gt;
|-&lt;br /&gt;
| \D&lt;br /&gt;
| non-digit character&lt;br /&gt;
|-&lt;br /&gt;
| \b&lt;br /&gt;
| backspace (0x08) (only if in a range specification)&lt;br /&gt;
|-&lt;br /&gt;
| \B&lt;br /&gt;
| non-word boundary&lt;br /&gt;
|-&lt;br /&gt;
|{m,n}	 &lt;br /&gt;
|at least m and at most n repetitions of the preceding &lt;br /&gt;
|&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++], letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in [http://en.wikipedia.org/wiki/Java_(programming_language) Java].&lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
The comparable mixin is used by classes whose objects may be ordered.The 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. if the other object is not comparable then is should just return nil.Comparable uses &amp;lt;=&amp;gt; to implement the comparison operators(&amp;lt;,&amp;lt;=,==,=&amp;gt;,and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
Here is an example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate comparable mixin.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line &lt;br /&gt;
  def initialize(x1, y1, x2, y2) &lt;br /&gt;
    @x1, @y1, @x2, @y2 = x1, y1, x2, y2 &lt;br /&gt;
  end   &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We compare two lines on the basis of their lengths.&lt;br /&gt;
We add the Comparable mixin as follows: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line  &lt;br /&gt;
  include Comparable  &lt;br /&gt;
  def length_squared &lt;br /&gt;
    (@x2-@x1) * (@x2-@x1) + (@y2-@y1) * (@y2-@y1)&lt;br /&gt;
end&lt;br /&gt;
def &amp;lt;=&amp;gt;(other) &lt;br /&gt;
    self.length_squared &amp;lt;=&amp;gt; other.length_squared &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;=&amp;gt; returns 1,0, or –1, depending on  whether the first argument is &lt;br /&gt;
greater than, equal to, or less than the second argument.  &lt;br /&gt;
We delegate the call to &amp;lt;=&amp;gt; of the Fixnum class, which compares &lt;br /&gt;
the squares of the lengths. &lt;br /&gt;
Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
l1 = Line.new(1, 0, 4, 3) &lt;br /&gt;
l2 = Line.new(0, 0, 10, 10) &lt;br /&gt;
puts l1.length_squared &lt;br /&gt;
if l1 &amp;lt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is shorter than Line 2&amp;quot; &lt;br /&gt;
else if l1 &amp;gt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is longer than Line 2&amp;quot; &lt;br /&gt;
else &lt;br /&gt;
  puts &amp;quot;Line 1 is just as long as Line 2&amp;quot; &lt;br /&gt;
 end &lt;br /&gt;
end &lt;br /&gt;
&amp;gt;&amp;gt;Line 1 is shorter than Line 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
Enumerable is a standard mixin which can be used to put into other classes.It has a method called inject that can be applied to adjacent elements of a set.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[1, 2, 3, 4, 5 ,6].inject {|v, n| v+n }&lt;br /&gt;
&amp;gt;&amp;gt;21&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen in the above example inject is used on all elements of the set to add them and display the result.&lt;br /&gt;
&lt;br /&gt;
To understand the working of Enumerable mixin in class,consider the following example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class VowelFinder &lt;br /&gt;
  include Enumerable &lt;br /&gt;
  def initialize(string) &lt;br /&gt;
    @string = string &lt;br /&gt;
  end &lt;br /&gt;
  def each &lt;br /&gt;
    @string.scan(/[aeiou]/) do |vowel| &lt;br /&gt;
      yield vowel &lt;br /&gt;
    end &lt;br /&gt;
  end &lt;br /&gt;
end  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
VowelFinder.new(&amp;quot;abacadabra&amp;quot;).inject {|v, n| v+n} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] has several disadvantages that can lead to ambiguous code behavior either during [http://en.wikipedia.org/wiki/Compile_time compile time] or [http://en.wikipedia.org/wiki/Run_time run time]. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules]. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
      puts &amp;quot;Inside a1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
      puts &amp;quot;Inside a2&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
       puts &amp;quot;Inside b1&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
       puts &amp;quot;Inside b2&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
      puts &amp;quot;Inside s1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1             # =&amp;gt; Inside a1&lt;br /&gt;
  obj.a2             # =&amp;gt; Inside a2&lt;br /&gt;
  obj.b1             # =&amp;gt; Inside b1&lt;br /&gt;
  obj.b2             # =&amp;gt; Inside b2&lt;br /&gt;
  obj.s1             # =&amp;gt; Inside s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses super-classes].&amp;lt;br&amp;gt;&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super-classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses sub-class].&amp;lt;br&amp;gt; &lt;br /&gt;
For example, [http://en.wikipedia.org/wiki/Mixin Mixins] help achieve this.&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by [http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html implementing the interfaces]. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase [http://en.wikipedia.org/wiki/Reusability reusability] and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
Some people call this situation the '''&amp;quot;[http://en.wikipedia.org/wiki/Diamond_problem Deadly Diamond of Death]&amp;quot;'''. This is illustrated by the Java code example below:&lt;br /&gt;
&lt;br /&gt;
  // This is our top most abstract class.&lt;br /&gt;
  class AbstractSuperClass{&lt;br /&gt;
    abstract void method();&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // These are the two concrete sub classes which extend the above super class&lt;br /&gt;
  class ConcreteSubOne extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I am going to test multiple Inheritance&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  class ConcreteSubTwo extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I will cause the Deadly Diamond of Death&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  // This is our last class which extends both of the above concrete classes&lt;br /&gt;
  class DeadlyDiamondEffect extends ConcreteSubOne, ConcreteSubTwo{&lt;br /&gt;
    //Some methods of this class&lt;br /&gt;
    //This inherits a do() method from ConcreteSubOne and another do() from ConcreteSubTwo. When this is called there is an ambiguity.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a [http://en.wikipedia.org/wiki/Object_composition object composition] but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;[http://en.wikipedia.org/wiki/Is-a Is-a]&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming renaming].&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://strugglingwithruby.blogspot.com/2009/05/regular-expressions-in-ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/regexp.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/modules_mixins.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://javacodeonline.blogspot.com/2009/08/deadly-diamond-of-death.html Deadly Diamond of Death]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem&lt;br /&gt;
[http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses Subclasses and Superclasses]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&amp;lt;br&amp;gt;&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &amp;lt;br&amp;gt;&lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.java2s.com/Code/Ruby/Class/Rubytermsmultipleinheritanceamixin Ruby Multiple Inheritance with modules and mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &amp;lt;br&amp;gt;&lt;br /&gt;
http://rubyonrails.org/ &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53082</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53082"/>
		<updated>2011-10-20T16:38:09Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Comparable */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] was built as a better [http://en.wikipedia.org/wiki/Perl Perl] hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
&amp;gt;&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Character&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| []&lt;br /&gt;
| range specificication (e.g., [a-z] means a letter in the range a to z)&lt;br /&gt;
|-&lt;br /&gt;
| \w &lt;br /&gt;
| word character; same as [0-9A-Za-z_]&lt;br /&gt;
|-&lt;br /&gt;
| \W&lt;br /&gt;
| non-word character&lt;br /&gt;
|-&lt;br /&gt;
| \s&lt;br /&gt;
| space character; same as [ \t\n\r\f]&lt;br /&gt;
|-&lt;br /&gt;
| \S&lt;br /&gt;
| non-space character&lt;br /&gt;
|-&lt;br /&gt;
| \d&lt;br /&gt;
| digit character; same as [0-9]&lt;br /&gt;
|-&lt;br /&gt;
| \D&lt;br /&gt;
| non-digit character&lt;br /&gt;
|-&lt;br /&gt;
| \b&lt;br /&gt;
| backspace (0x08) (only if in a range specification)&lt;br /&gt;
|-&lt;br /&gt;
| \B&lt;br /&gt;
| non-word boundary&lt;br /&gt;
|-&lt;br /&gt;
|{m,n}	 &lt;br /&gt;
|at least m and at most n repetitions of the preceding &lt;br /&gt;
|&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++], letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in [http://en.wikipedia.org/wiki/Java_(programming_language) Java].&lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
The comparable mixin is used by classes whose objects may be ordered.The 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. if the other object is not comparable then is should just return nil.Comparable uses &amp;lt;=&amp;gt; to implement the comparison operators(&amp;lt;,&amp;lt;=,==,=&amp;gt;,and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
Here is an example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate comparable mixin.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line &lt;br /&gt;
  def initialize(x1, y1, x2, y2) &lt;br /&gt;
    @x1, @y1, @x2, @y2 = x1, y1, x2, y2 &lt;br /&gt;
  end   &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We compare two lines on the basis of their lengths.&lt;br /&gt;
We add the Comparable mixin as follows: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line  &lt;br /&gt;
  include Comparable  &lt;br /&gt;
  def length_squared &lt;br /&gt;
    (@x2-@x1) * (@x2-@x1) + (@y2-@y1) * (@y2-@y1)&lt;br /&gt;
end&lt;br /&gt;
def &amp;lt;=&amp;gt;(other) &lt;br /&gt;
    self.length_squared &amp;lt;=&amp;gt; other.length_squared &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;=&amp;gt; returns 1,0, or –1, depending on  whether the first argument is &lt;br /&gt;
greater than, equal to, or less than the second argument.  &lt;br /&gt;
We delegate the call to &amp;lt;=&amp;gt; of the Fixnum class, which compares &lt;br /&gt;
the squares of the lengths. &lt;br /&gt;
Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
l1 = Line.new(1, 0, 4, 3) &lt;br /&gt;
l2 = Line.new(0, 0, 10, 10) &lt;br /&gt;
puts l1.length_squared &lt;br /&gt;
if l1 &amp;lt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is shorter than Line 2&amp;quot; &lt;br /&gt;
else if l1 &amp;gt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is longer than Line 2&amp;quot; &lt;br /&gt;
else &lt;br /&gt;
  puts &amp;quot;Line 1 is just as long as Line 2&amp;quot; &lt;br /&gt;
 end &lt;br /&gt;
end &lt;br /&gt;
&amp;gt;&amp;gt;Line 1 is shorter than Line 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
Enumerable is a standard mixin which can be used to put into other classes.It has a method called inject that can be applied to adjacent elements of a set.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[1, 2, 3, 4, 5].inject {|v, n| v+n }&lt;br /&gt;
&amp;gt;&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen in the above example inject is used on all elements of the set to add them and display the result.&lt;br /&gt;
&lt;br /&gt;
To understand the working of Enumerable mixin in class,consider the following example. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class VowelFinder &lt;br /&gt;
  include Enumerable &lt;br /&gt;
  def initialize(string) &lt;br /&gt;
    @string = string &lt;br /&gt;
  end &lt;br /&gt;
  def each &lt;br /&gt;
    @string.scan(/[aeiou]/) do |vowel| &lt;br /&gt;
      yield vowel &lt;br /&gt;
    end &lt;br /&gt;
  end &lt;br /&gt;
end  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
VowelFinder.new(&amp;quot;abacadabra&amp;quot;).inject {|v, n| v+n} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] has several disadvantages that can lead to ambiguous code behavior either during [http://en.wikipedia.org/wiki/Compile_time compile time] or [http://en.wikipedia.org/wiki/Run_time run time]. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules]. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
      puts &amp;quot;Inside a1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
      puts &amp;quot;Inside a2&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
       puts &amp;quot;Inside b1&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
       puts &amp;quot;Inside b2&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
      puts &amp;quot;Inside s1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1             # =&amp;gt; Inside a1&lt;br /&gt;
  obj.a2             # =&amp;gt; Inside a2&lt;br /&gt;
  obj.b1             # =&amp;gt; Inside b1&lt;br /&gt;
  obj.b2             # =&amp;gt; Inside b2&lt;br /&gt;
  obj.s1             # =&amp;gt; Inside s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses super-classes].&amp;lt;br&amp;gt;&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super-classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses sub-class].&amp;lt;br&amp;gt; &lt;br /&gt;
For example, [http://en.wikipedia.org/wiki/Mixin Mixins] help achieve this.&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by [http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html implementing the interfaces]. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase [http://en.wikipedia.org/wiki/Reusability reusability] and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
Some people call this situation the '''&amp;quot;[http://en.wikipedia.org/wiki/Diamond_problem Deadly Diamond of Death]&amp;quot;'''. This is illustrated by the Java code example below:&lt;br /&gt;
&lt;br /&gt;
  // This is our top most abstract class.&lt;br /&gt;
  class AbstractSuperClass{&lt;br /&gt;
    abstract void method();&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // These are the two concrete sub classes which extend the above super class&lt;br /&gt;
  class ConcreteSubOne extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I am going to test multiple Inheritance&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  class ConcreteSubTwo extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I will cause the Deadly Diamond of Death&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  // This is our last class which extends both of the above concrete classes&lt;br /&gt;
  class DeadlyDiamondEffect extends ConcreteSubOne, ConcreteSubTwo{&lt;br /&gt;
    //Some methods of this class&lt;br /&gt;
    //This inherits a do() method from ConcreteSubOne and another do() from ConcreteSubTwo. When this is called there is an ambiguity.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a [http://en.wikipedia.org/wiki/Object_composition object composition] but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;[http://en.wikipedia.org/wiki/Is-a Is-a]&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming renaming].&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://strugglingwithruby.blogspot.com/2009/05/regular-expressions-in-ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/regexp.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/modules_mixins.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://javacodeonline.blogspot.com/2009/08/deadly-diamond-of-death.html Deadly Diamond of Death]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem&lt;br /&gt;
[http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses Subclasses and Superclasses]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&amp;lt;br&amp;gt;&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &amp;lt;br&amp;gt;&lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.java2s.com/Code/Ruby/Class/Rubytermsmultipleinheritanceamixin Ruby Multiple Inheritance with modules and mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &amp;lt;br&amp;gt;&lt;br /&gt;
http://rubyonrails.org/ &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53081</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53081"/>
		<updated>2011-10-20T16:37:35Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] was built as a better [http://en.wikipedia.org/wiki/Perl Perl] hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
&amp;gt;&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Character&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| []&lt;br /&gt;
| range specificication (e.g., [a-z] means a letter in the range a to z)&lt;br /&gt;
|-&lt;br /&gt;
| \w &lt;br /&gt;
| word character; same as [0-9A-Za-z_]&lt;br /&gt;
|-&lt;br /&gt;
| \W&lt;br /&gt;
| non-word character&lt;br /&gt;
|-&lt;br /&gt;
| \s&lt;br /&gt;
| space character; same as [ \t\n\r\f]&lt;br /&gt;
|-&lt;br /&gt;
| \S&lt;br /&gt;
| non-space character&lt;br /&gt;
|-&lt;br /&gt;
| \d&lt;br /&gt;
| digit character; same as [0-9]&lt;br /&gt;
|-&lt;br /&gt;
| \D&lt;br /&gt;
| non-digit character&lt;br /&gt;
|-&lt;br /&gt;
| \b&lt;br /&gt;
| backspace (0x08) (only if in a range specification)&lt;br /&gt;
|-&lt;br /&gt;
| \B&lt;br /&gt;
| non-word boundary&lt;br /&gt;
|-&lt;br /&gt;
|{m,n}	 &lt;br /&gt;
|at least m and at most n repetitions of the preceding &lt;br /&gt;
|&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++], letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in [http://en.wikipedia.org/wiki/Java_(programming_language) Java].&lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
The comparable mixin is used by classes whose objects may be ordered.The 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. if the other object is not comparable then is should just return nil.Comparable uses &amp;lt;=&amp;gt; to implement the comparison operators(&amp;lt;,&amp;lt;=,==,=&amp;gt;,and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
Here is an example to illustrate comparable mixin.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line &lt;br /&gt;
  def initialize(x1, y1, x2, y2) &lt;br /&gt;
    @x1, @y1, @x2, @y2 = x1, y1, x2, y2 &lt;br /&gt;
  end   &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We compare two lines on the basis of their lengths.&lt;br /&gt;
We add the Comparable mixin as follows: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line  &lt;br /&gt;
  include Comparable  &lt;br /&gt;
  def length_squared &lt;br /&gt;
    (@x2-@x1) * (@x2-@x1) + (@y2-@y1) * (@y2-@y1)&lt;br /&gt;
end&lt;br /&gt;
def &amp;lt;=&amp;gt;(other) &lt;br /&gt;
    self.length_squared &amp;lt;=&amp;gt; other.length_squared &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;=&amp;gt; returns 1,0, or –1, depending on  whether the first argument is &lt;br /&gt;
greater than, equal to, or less than the second argument.  &lt;br /&gt;
We delegate the call to &amp;lt;=&amp;gt; of the Fixnum class, which compares &lt;br /&gt;
the squares of the lengths. &lt;br /&gt;
Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
l1 = Line.new(1, 0, 4, 3) &lt;br /&gt;
l2 = Line.new(0, 0, 10, 10) &lt;br /&gt;
puts l1.length_squared &lt;br /&gt;
if l1 &amp;lt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is shorter than Line 2&amp;quot; &lt;br /&gt;
else if l1 &amp;gt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is longer than Line 2&amp;quot; &lt;br /&gt;
else &lt;br /&gt;
  puts &amp;quot;Line 1 is just as long as Line 2&amp;quot; &lt;br /&gt;
 end &lt;br /&gt;
end &lt;br /&gt;
&amp;gt;&amp;gt;Line 1 is shorter than Line 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
Enumerable is a standard mixin which can be used to put into other classes.It has a method called inject that can be applied to adjacent elements of a set.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[1, 2, 3, 4, 5].inject {|v, n| v+n }&lt;br /&gt;
&amp;gt;&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen in the above example inject is used on all elements of the set to add them and display the result.&lt;br /&gt;
&lt;br /&gt;
To understand the working of Enumerable mixin in class,consider the following example. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class VowelFinder &lt;br /&gt;
  include Enumerable &lt;br /&gt;
  def initialize(string) &lt;br /&gt;
    @string = string &lt;br /&gt;
  end &lt;br /&gt;
  def each &lt;br /&gt;
    @string.scan(/[aeiou]/) do |vowel| &lt;br /&gt;
      yield vowel &lt;br /&gt;
    end &lt;br /&gt;
  end &lt;br /&gt;
end  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
VowelFinder.new(&amp;quot;abacadabra&amp;quot;).inject {|v, n| v+n} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] has several disadvantages that can lead to ambiguous code behavior either during [http://en.wikipedia.org/wiki/Compile_time compile time] or [http://en.wikipedia.org/wiki/Run_time run time]. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules]. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
      puts &amp;quot;Inside a1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
      puts &amp;quot;Inside a2&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
       puts &amp;quot;Inside b1&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
       puts &amp;quot;Inside b2&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
      puts &amp;quot;Inside s1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1             # =&amp;gt; Inside a1&lt;br /&gt;
  obj.a2             # =&amp;gt; Inside a2&lt;br /&gt;
  obj.b1             # =&amp;gt; Inside b1&lt;br /&gt;
  obj.b2             # =&amp;gt; Inside b2&lt;br /&gt;
  obj.s1             # =&amp;gt; Inside s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses super-classes].&amp;lt;br&amp;gt;&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super-classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses sub-class].&amp;lt;br&amp;gt; &lt;br /&gt;
For example, [http://en.wikipedia.org/wiki/Mixin Mixins] help achieve this.&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by [http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html implementing the interfaces]. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase [http://en.wikipedia.org/wiki/Reusability reusability] and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
Some people call this situation the '''&amp;quot;[http://en.wikipedia.org/wiki/Diamond_problem Deadly Diamond of Death]&amp;quot;'''. This is illustrated by the Java code example below:&lt;br /&gt;
&lt;br /&gt;
  // This is our top most abstract class.&lt;br /&gt;
  class AbstractSuperClass{&lt;br /&gt;
    abstract void method();&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // These are the two concrete sub classes which extend the above super class&lt;br /&gt;
  class ConcreteSubOne extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I am going to test multiple Inheritance&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  class ConcreteSubTwo extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I will cause the Deadly Diamond of Death&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  // This is our last class which extends both of the above concrete classes&lt;br /&gt;
  class DeadlyDiamondEffect extends ConcreteSubOne, ConcreteSubTwo{&lt;br /&gt;
    //Some methods of this class&lt;br /&gt;
    //This inherits a do() method from ConcreteSubOne and another do() from ConcreteSubTwo. When this is called there is an ambiguity.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a [http://en.wikipedia.org/wiki/Object_composition object composition] but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;[http://en.wikipedia.org/wiki/Is-a Is-a]&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming renaming].&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://strugglingwithruby.blogspot.com/2009/05/regular-expressions-in-ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/regexp.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/modules_mixins.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://javacodeonline.blogspot.com/2009/08/deadly-diamond-of-death.html Deadly Diamond of Death]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem&lt;br /&gt;
[http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses Subclasses and Superclasses]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&amp;lt;br&amp;gt;&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &amp;lt;br&amp;gt;&lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.java2s.com/Code/Ruby/Class/Rubytermsmultipleinheritanceamixin Ruby Multiple Inheritance with modules and mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &amp;lt;br&amp;gt;&lt;br /&gt;
http://rubyonrails.org/ &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53080</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53080"/>
		<updated>2011-10-20T16:36:45Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] was built as a better [http://en.wikipedia.org/wiki/Perl Perl] hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
&amp;gt;&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Character&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| []&lt;br /&gt;
| range specificication (e.g., [a-z] means a letter in the range a to z)&lt;br /&gt;
|-&lt;br /&gt;
| \w &lt;br /&gt;
| word character; same as [0-9A-Za-z_]&lt;br /&gt;
|-&lt;br /&gt;
| \W&lt;br /&gt;
| non-word character&lt;br /&gt;
|-&lt;br /&gt;
| \s&lt;br /&gt;
| space character; same as [ \t\n\r\f]&lt;br /&gt;
|-&lt;br /&gt;
| \S&lt;br /&gt;
| non-space character&lt;br /&gt;
|-&lt;br /&gt;
| \d&lt;br /&gt;
| digit character; same as [0-9]&lt;br /&gt;
|-&lt;br /&gt;
| \D&lt;br /&gt;
| non-digit character&lt;br /&gt;
|-&lt;br /&gt;
| \b&lt;br /&gt;
| backspace (0x08) (only if in a range specification)&lt;br /&gt;
|-&lt;br /&gt;
| \B&lt;br /&gt;
| non-word boundary&lt;br /&gt;
|-&lt;br /&gt;
|{m,n}	 &lt;br /&gt;
|at least m and at most n repetitions of the preceding &lt;br /&gt;
|&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++], letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example taken from class notes [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in [http://en.wikipedia.org/wiki/Java_(programming_language) Java].&lt;br /&gt;
&lt;br /&gt;
Consider the following example to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
The comparable mixin is used by classes whose objects may be ordered.The 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. if the other object is not comparable then is should just return nil.Comparable uses &amp;lt;=&amp;gt; to implement the comparison operators(&amp;lt;,&amp;lt;=,==,=&amp;gt;,and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
Here is an example to illustrate comparable mixin.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line &lt;br /&gt;
  def initialize(x1, y1, x2, y2) &lt;br /&gt;
    @x1, @y1, @x2, @y2 = x1, y1, x2, y2 &lt;br /&gt;
  end   &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We compare two lines on the basis of their lengths.&lt;br /&gt;
We add the Comparable mixin as follows: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line  &lt;br /&gt;
  include Comparable  &lt;br /&gt;
  def length_squared &lt;br /&gt;
    (@x2-@x1) * (@x2-@x1) + (@y2-@y1) * (@y2-@y1)&lt;br /&gt;
end&lt;br /&gt;
def &amp;lt;=&amp;gt;(other) &lt;br /&gt;
    self.length_squared &amp;lt;=&amp;gt; other.length_squared &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;=&amp;gt; returns 1,0, or –1, depending on  whether the first argument is &lt;br /&gt;
greater than, equal to, or less than the second argument.  &lt;br /&gt;
We delegate the call to &amp;lt;=&amp;gt; of the Fixnum class, which compares &lt;br /&gt;
the squares of the lengths. &lt;br /&gt;
Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
l1 = Line.new(1, 0, 4, 3) &lt;br /&gt;
l2 = Line.new(0, 0, 10, 10) &lt;br /&gt;
puts l1.length_squared &lt;br /&gt;
if l1 &amp;lt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is shorter than Line 2&amp;quot; &lt;br /&gt;
else if l1 &amp;gt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is longer than Line 2&amp;quot; &lt;br /&gt;
else &lt;br /&gt;
  puts &amp;quot;Line 1 is just as long as Line 2&amp;quot; &lt;br /&gt;
 end &lt;br /&gt;
end &lt;br /&gt;
&amp;gt;&amp;gt;Line 1 is shorter than Line 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
Enumerable is a standard mixin which can be used to put into other classes.It has a method called inject that can be applied to adjacent elements of a set.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[1, 2, 3, 4, 5].inject {|v, n| v+n }&lt;br /&gt;
&amp;gt;&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen in the above example inject is used on all elements of the set to add them and display the result.&lt;br /&gt;
&lt;br /&gt;
To understand the working of Enumerable mixin in class,consider the following example. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class VowelFinder &lt;br /&gt;
  include Enumerable &lt;br /&gt;
  def initialize(string) &lt;br /&gt;
    @string = string &lt;br /&gt;
  end &lt;br /&gt;
  def each &lt;br /&gt;
    @string.scan(/[aeiou]/) do |vowel| &lt;br /&gt;
      yield vowel &lt;br /&gt;
    end &lt;br /&gt;
  end &lt;br /&gt;
end  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
VowelFinder.new(&amp;quot;abacadabra&amp;quot;).inject {|v, n| v+n} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] has several disadvantages that can lead to ambiguous code behavior either during [http://en.wikipedia.org/wiki/Compile_time compile time] or [http://en.wikipedia.org/wiki/Run_time run time]. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules]. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
      puts &amp;quot;Inside a1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
      puts &amp;quot;Inside a2&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
       puts &amp;quot;Inside b1&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
       puts &amp;quot;Inside b2&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
      puts &amp;quot;Inside s1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1             # =&amp;gt; Inside a1&lt;br /&gt;
  obj.a2             # =&amp;gt; Inside a2&lt;br /&gt;
  obj.b1             # =&amp;gt; Inside b1&lt;br /&gt;
  obj.b2             # =&amp;gt; Inside b2&lt;br /&gt;
  obj.s1             # =&amp;gt; Inside s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses super-classes].&amp;lt;br&amp;gt;&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super-classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses sub-class].&amp;lt;br&amp;gt; &lt;br /&gt;
For example, [http://en.wikipedia.org/wiki/Mixin Mixins] help achieve this.&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by [http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html implementing the interfaces]. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase [http://en.wikipedia.org/wiki/Reusability reusability] and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
Some people call this situation the '''&amp;quot;[http://en.wikipedia.org/wiki/Diamond_problem Deadly Diamond of Death]&amp;quot;'''. This is illustrated by the Java code example below:&lt;br /&gt;
&lt;br /&gt;
  // This is our top most abstract class.&lt;br /&gt;
  class AbstractSuperClass{&lt;br /&gt;
    abstract void method();&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // These are the two concrete sub classes which extend the above super class&lt;br /&gt;
  class ConcreteSubOne extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I am going to test multiple Inheritance&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  class ConcreteSubTwo extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I will cause the Deadly Diamond of Death&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  // This is our last class which extends both of the above concrete classes&lt;br /&gt;
  class DeadlyDiamondEffect extends ConcreteSubOne, ConcreteSubTwo{&lt;br /&gt;
    //Some methods of this class&lt;br /&gt;
    //This inherits a do() method from ConcreteSubOne and another do() from ConcreteSubTwo. When this is called there is an ambiguity.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a [http://en.wikipedia.org/wiki/Object_composition object composition] but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;[http://en.wikipedia.org/wiki/Is-a Is-a]&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming renaming].&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://strugglingwithruby.blogspot.com/2009/05/regular-expressions-in-ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/regexp.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/modules_mixins.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://javacodeonline.blogspot.com/2009/08/deadly-diamond-of-death.html Deadly Diamond of Death]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem&lt;br /&gt;
[http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses Subclasses and Superclasses]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&amp;lt;br&amp;gt;&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &amp;lt;br&amp;gt;&lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.java2s.com/Code/Ruby/Class/Rubytermsmultipleinheritanceamixin Ruby Multiple Inheritance with modules and mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &amp;lt;br&amp;gt;&lt;br /&gt;
http://rubyonrails.org/ &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53079</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53079"/>
		<updated>2011-10-20T16:36:11Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] was built as a better [http://en.wikipedia.org/wiki/Perl Perl] hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
&amp;gt;&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Character&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| []&lt;br /&gt;
| range specificication (e.g., [a-z] means a letter in the range a to z)&lt;br /&gt;
|-&lt;br /&gt;
| \w &lt;br /&gt;
| word character; same as [0-9A-Za-z_]&lt;br /&gt;
|-&lt;br /&gt;
| \W&lt;br /&gt;
| non-word character&lt;br /&gt;
|-&lt;br /&gt;
| \s&lt;br /&gt;
| space character; same as [ \t\n\r\f]&lt;br /&gt;
|-&lt;br /&gt;
| \S&lt;br /&gt;
| non-space character&lt;br /&gt;
|-&lt;br /&gt;
| \d&lt;br /&gt;
| digit character; same as [0-9]&lt;br /&gt;
|-&lt;br /&gt;
| \D&lt;br /&gt;
| non-digit character&lt;br /&gt;
|-&lt;br /&gt;
| \b&lt;br /&gt;
| backspace (0x08) (only if in a range specification)&lt;br /&gt;
|-&lt;br /&gt;
| \B&lt;br /&gt;
| non-word boundary&lt;br /&gt;
|-&lt;br /&gt;
|{m,n}	 &lt;br /&gt;
|at least m and at most n repetitions of the preceding &lt;br /&gt;
|&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++], letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example from to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in [http://en.wikipedia.org/wiki/Java_(programming_language) Java].&lt;br /&gt;
&lt;br /&gt;
Consider the following example to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
The comparable mixin is used by classes whose objects may be ordered.The 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. if the other object is not comparable then is should just return nil.Comparable uses &amp;lt;=&amp;gt; to implement the comparison operators(&amp;lt;,&amp;lt;=,==,=&amp;gt;,and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
Here is an example to illustrate comparable mixin.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line &lt;br /&gt;
  def initialize(x1, y1, x2, y2) &lt;br /&gt;
    @x1, @y1, @x2, @y2 = x1, y1, x2, y2 &lt;br /&gt;
  end   &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We compare two lines on the basis of their lengths.&lt;br /&gt;
We add the Comparable mixin as follows: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line  &lt;br /&gt;
  include Comparable  &lt;br /&gt;
  def length_squared &lt;br /&gt;
    (@x2-@x1) * (@x2-@x1) + (@y2-@y1) * (@y2-@y1)&lt;br /&gt;
end&lt;br /&gt;
def &amp;lt;=&amp;gt;(other) &lt;br /&gt;
    self.length_squared &amp;lt;=&amp;gt; other.length_squared &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;=&amp;gt; returns 1,0, or –1, depending on  whether the first argument is &lt;br /&gt;
greater than, equal to, or less than the second argument.  &lt;br /&gt;
We delegate the call to &amp;lt;=&amp;gt; of the Fixnum class, which compares &lt;br /&gt;
the squares of the lengths. &lt;br /&gt;
Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
l1 = Line.new(1, 0, 4, 3) &lt;br /&gt;
l2 = Line.new(0, 0, 10, 10) &lt;br /&gt;
puts l1.length_squared &lt;br /&gt;
if l1 &amp;lt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is shorter than Line 2&amp;quot; &lt;br /&gt;
else if l1 &amp;gt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is longer than Line 2&amp;quot; &lt;br /&gt;
else &lt;br /&gt;
  puts &amp;quot;Line 1 is just as long as Line 2&amp;quot; &lt;br /&gt;
 end &lt;br /&gt;
end &lt;br /&gt;
&amp;gt;&amp;gt;Line 1 is shorter than Line 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
Enumerable is a standard mixin which can be used to put into other classes.It has a method called inject that can be applied to adjacent elements of a set.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[1, 2, 3, 4, 5].inject {|v, n| v+n }&lt;br /&gt;
&amp;gt;&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen in the above example inject is used on all elements of the set to add them and display the result.&lt;br /&gt;
&lt;br /&gt;
To understand the working of Enumerable mixin in class,consider the following example. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class VowelFinder &lt;br /&gt;
  include Enumerable &lt;br /&gt;
  def initialize(string) &lt;br /&gt;
    @string = string &lt;br /&gt;
  end &lt;br /&gt;
  def each &lt;br /&gt;
    @string.scan(/[aeiou]/) do |vowel| &lt;br /&gt;
      yield vowel &lt;br /&gt;
    end &lt;br /&gt;
  end &lt;br /&gt;
end  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
VowelFinder.new(&amp;quot;abacadabra&amp;quot;).inject {|v, n| v+n} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] has several disadvantages that can lead to ambiguous code behavior either during [http://en.wikipedia.org/wiki/Compile_time compile time] or [http://en.wikipedia.org/wiki/Run_time run time]. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules]. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
      puts &amp;quot;Inside a1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
      puts &amp;quot;Inside a2&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
       puts &amp;quot;Inside b1&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
       puts &amp;quot;Inside b2&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
      puts &amp;quot;Inside s1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1             # =&amp;gt; Inside a1&lt;br /&gt;
  obj.a2             # =&amp;gt; Inside a2&lt;br /&gt;
  obj.b1             # =&amp;gt; Inside b1&lt;br /&gt;
  obj.b2             # =&amp;gt; Inside b2&lt;br /&gt;
  obj.s1             # =&amp;gt; Inside s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses super-classes].&amp;lt;br&amp;gt;&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super-classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses sub-class].&amp;lt;br&amp;gt; &lt;br /&gt;
For example, [http://en.wikipedia.org/wiki/Mixin Mixins] help achieve this.&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by [http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html implementing the interfaces]. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase [http://en.wikipedia.org/wiki/Reusability reusability] and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
Some people call this situation the '''&amp;quot;[http://en.wikipedia.org/wiki/Diamond_problem Deadly Diamond of Death]&amp;quot;'''. This is illustrated by the Java code example below:&lt;br /&gt;
&lt;br /&gt;
  // This is our top most abstract class.&lt;br /&gt;
  class AbstractSuperClass{&lt;br /&gt;
    abstract void method();&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // These are the two concrete sub classes which extend the above super class&lt;br /&gt;
  class ConcreteSubOne extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I am going to test multiple Inheritance&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  class ConcreteSubTwo extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I will cause the Deadly Diamond of Death&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  // This is our last class which extends both of the above concrete classes&lt;br /&gt;
  class DeadlyDiamondEffect extends ConcreteSubOne, ConcreteSubTwo{&lt;br /&gt;
    //Some methods of this class&lt;br /&gt;
    //This inherits a do() method from ConcreteSubOne and another do() from ConcreteSubTwo. When this is called there is an ambiguity.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a [http://en.wikipedia.org/wiki/Object_composition object composition] but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;[http://en.wikipedia.org/wiki/Is-a Is-a]&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming renaming].&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://strugglingwithruby.blogspot.com/2009/05/regular-expressions-in-ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/regexp.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/modules_mixins.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://javacodeonline.blogspot.com/2009/08/deadly-diamond-of-death.html Deadly Diamond of Death]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem&lt;br /&gt;
[http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses Subclasses and Superclasses]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&amp;lt;br&amp;gt;&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &amp;lt;br&amp;gt;&lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.java2s.com/Code/Ruby/Class/Rubytermsmultipleinheritanceamixin Ruby Multiple Inheritance with modules and mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &amp;lt;br&amp;gt;&lt;br /&gt;
http://rubyonrails.org/ &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53078</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53078"/>
		<updated>2011-10-20T16:34:28Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] was built as a better [http://en.wikipedia.org/wiki/Perl Perl] hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
&amp;gt;&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Character&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| []&lt;br /&gt;
| range specificication (e.g., [a-z] means a letter in the range a to z)&lt;br /&gt;
|-&lt;br /&gt;
| \w &lt;br /&gt;
| word character; same as [0-9A-Za-z_]&lt;br /&gt;
|-&lt;br /&gt;
| \W&lt;br /&gt;
| non-word character&lt;br /&gt;
|-&lt;br /&gt;
| \s&lt;br /&gt;
| space character; same as [ \t\n\r\f]&lt;br /&gt;
|-&lt;br /&gt;
| \S&lt;br /&gt;
| non-space character&lt;br /&gt;
|-&lt;br /&gt;
| \d&lt;br /&gt;
| digit character; same as [0-9]&lt;br /&gt;
|-&lt;br /&gt;
| \D&lt;br /&gt;
| non-digit character&lt;br /&gt;
|-&lt;br /&gt;
| \b&lt;br /&gt;
| backspace (0x08) (only if in a range specification)&lt;br /&gt;
|-&lt;br /&gt;
| \B&lt;br /&gt;
| non-word boundary&lt;br /&gt;
|-&lt;br /&gt;
|{m,n}	 &lt;br /&gt;
|at least m and at most n repetitions of the preceding &lt;br /&gt;
|&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++], letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in [http://en.wikipedia.org/wiki/Java_(programming_language) Java].&lt;br /&gt;
&lt;br /&gt;
Consider the following example to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
The comparable mixin is used by classes whose objects may be ordered.The 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. if the other object is not comparable then is should just return nil.Comparable uses &amp;lt;=&amp;gt; to implement the comparison operators(&amp;lt;,&amp;lt;=,==,=&amp;gt;,and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
Here is an example to illustrate comparable mixin.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line &lt;br /&gt;
  def initialize(x1, y1, x2, y2) &lt;br /&gt;
    @x1, @y1, @x2, @y2 = x1, y1, x2, y2 &lt;br /&gt;
  end   &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We compare two lines on the basis of their lengths.&lt;br /&gt;
We add the Comparable mixin as follows: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line  &lt;br /&gt;
  include Comparable  &lt;br /&gt;
  def length_squared &lt;br /&gt;
    (@x2-@x1) * (@x2-@x1) + (@y2-@y1) * (@y2-@y1)&lt;br /&gt;
end&lt;br /&gt;
def &amp;lt;=&amp;gt;(other) &lt;br /&gt;
    self.length_squared &amp;lt;=&amp;gt; other.length_squared &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;=&amp;gt; returns 1,0, or –1, depending on  whether the first argument is &lt;br /&gt;
greater than, equal to, or less than the second argument.  &lt;br /&gt;
We delegate the call to &amp;lt;=&amp;gt; of the Fixnum class, which compares &lt;br /&gt;
the squares of the lengths. &lt;br /&gt;
Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
l1 = Line.new(1, 0, 4, 3) &lt;br /&gt;
l2 = Line.new(0, 0, 10, 10) &lt;br /&gt;
puts l1.length_squared &lt;br /&gt;
if l1 &amp;lt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is shorter than Line 2&amp;quot; &lt;br /&gt;
else if l1 &amp;gt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is longer than Line 2&amp;quot; &lt;br /&gt;
else &lt;br /&gt;
  puts &amp;quot;Line 1 is just as long as Line 2&amp;quot; &lt;br /&gt;
 end &lt;br /&gt;
end &lt;br /&gt;
&amp;gt;&amp;gt;Line 1 is shorter than Line 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
Enumerable is a standard mixin which can be used to put into other classes.It has a method called inject that can be applied to adjacent elements of a set.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[1, 2, 3, 4, 5].inject {|v, n| v+n }&lt;br /&gt;
&amp;gt;&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen in the above example inject is used on all elements of the set to add them and display the result.&lt;br /&gt;
&lt;br /&gt;
To understand the working of Enumerable mixin in class,consider the following example. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class VowelFinder &lt;br /&gt;
  include Enumerable &lt;br /&gt;
  def initialize(string) &lt;br /&gt;
    @string = string &lt;br /&gt;
  end &lt;br /&gt;
  def each &lt;br /&gt;
    @string.scan(/[aeiou]/) do |vowel| &lt;br /&gt;
      yield vowel &lt;br /&gt;
    end &lt;br /&gt;
  end &lt;br /&gt;
end  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
VowelFinder.new(&amp;quot;abacadabra&amp;quot;).inject {|v, n| v+n} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] has several disadvantages that can lead to ambiguous code behavior either during [http://en.wikipedia.org/wiki/Compile_time compile time] or [http://en.wikipedia.org/wiki/Run_time run time]. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules]. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
      puts &amp;quot;Inside a1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
      puts &amp;quot;Inside a2&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
       puts &amp;quot;Inside b1&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
       puts &amp;quot;Inside b2&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
      puts &amp;quot;Inside s1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1             # =&amp;gt; Inside a1&lt;br /&gt;
  obj.a2             # =&amp;gt; Inside a2&lt;br /&gt;
  obj.b1             # =&amp;gt; Inside b1&lt;br /&gt;
  obj.b2             # =&amp;gt; Inside b2&lt;br /&gt;
  obj.s1             # =&amp;gt; Inside s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses super-classes].&amp;lt;br&amp;gt;&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super-classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses sub-class].&amp;lt;br&amp;gt; &lt;br /&gt;
For example, [http://en.wikipedia.org/wiki/Mixin Mixins] help achieve this.&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by [http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html implementing the interfaces]. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase [http://en.wikipedia.org/wiki/Reusability reusability] and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
Some people call this situation the '''&amp;quot;[http://en.wikipedia.org/wiki/Diamond_problem Deadly Diamond of Death]&amp;quot;'''. This is illustrated by the Java code example below:&lt;br /&gt;
&lt;br /&gt;
  // This is our top most abstract class.&lt;br /&gt;
  class AbstractSuperClass{&lt;br /&gt;
    abstract void method();&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // These are the two concrete sub classes which extend the above super class&lt;br /&gt;
  class ConcreteSubOne extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I am going to test multiple Inheritance&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  class ConcreteSubTwo extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I will cause the Deadly Diamond of Death&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  // This is our last class which extends both of the above concrete classes&lt;br /&gt;
  class DeadlyDiamondEffect extends ConcreteSubOne, ConcreteSubTwo{&lt;br /&gt;
    //Some methods of this class&lt;br /&gt;
    //This inherits a do() method from ConcreteSubOne and another do() from ConcreteSubTwo. When this is called there is an ambiguity.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a [http://en.wikipedia.org/wiki/Object_composition object composition] but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;[http://en.wikipedia.org/wiki/Is-a Is-a]&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming renaming].&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://strugglingwithruby.blogspot.com/2009/05/regular-expressions-in-ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.rubyist.net/~slagell/ruby/regexp.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/modules_mixins.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://javacodeonline.blogspot.com/2009/08/deadly-diamond-of-death.html Deadly Diamond of Death]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem&lt;br /&gt;
[http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses Subclasses and Superclasses]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&amp;lt;br&amp;gt;&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &amp;lt;br&amp;gt;&lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.java2s.com/Code/Ruby/Class/Rubytermsmultipleinheritanceamixin Ruby Multiple Inheritance with modules and mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &amp;lt;br&amp;gt;&lt;br /&gt;
http://rubyonrails.org/ &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53076</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53076"/>
		<updated>2011-10-20T16:32:50Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] was built as a better [http://en.wikipedia.org/wiki/Perl Perl] hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
&amp;gt;&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Character&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| []&lt;br /&gt;
| range specificication (e.g., [a-z] means a letter in the range a to z)&lt;br /&gt;
|-&lt;br /&gt;
| \w &lt;br /&gt;
| word character; same as [0-9A-Za-z_]&lt;br /&gt;
|-&lt;br /&gt;
| \W&lt;br /&gt;
| non-word character&lt;br /&gt;
|-&lt;br /&gt;
| \s&lt;br /&gt;
| space character; same as [ \t\n\r\f]&lt;br /&gt;
|-&lt;br /&gt;
| \S&lt;br /&gt;
| non-space character&lt;br /&gt;
|-&lt;br /&gt;
| \d&lt;br /&gt;
| digit character; same as [0-9]&lt;br /&gt;
|-&lt;br /&gt;
| \D&lt;br /&gt;
| non-digit character&lt;br /&gt;
|-&lt;br /&gt;
| \b&lt;br /&gt;
| backspace (0x08) (only if in a range specification)&lt;br /&gt;
|-&lt;br /&gt;
| \B&lt;br /&gt;
| non-word boundary&lt;br /&gt;
|-&lt;br /&gt;
|{m,n}	 &lt;br /&gt;
|at least m and at most n repetitions of the preceding &lt;br /&gt;
|&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++], letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance] in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in [http://en.wikipedia.org/wiki/Java_(programming_language) Java].&lt;br /&gt;
&lt;br /&gt;
Consider the following example to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
The comparable mixin is used by classes whose objects may be ordered.The 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. if the other object is not comparable then is should just return nil.Comparable uses &amp;lt;=&amp;gt; to implement the comparison operators(&amp;lt;,&amp;lt;=,==,=&amp;gt;,and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
Here is an example to illustrate comparable mixin.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line &lt;br /&gt;
  def initialize(x1, y1, x2, y2) &lt;br /&gt;
    @x1, @y1, @x2, @y2 = x1, y1, x2, y2 &lt;br /&gt;
  end   &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We compare two lines on the basis of their lengths.&lt;br /&gt;
We add the Comparable mixin as follows: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line  &lt;br /&gt;
  include Comparable  &lt;br /&gt;
  def length_squared &lt;br /&gt;
    (@x2-@x1) * (@x2-@x1) + (@y2-@y1) * (@y2-@y1)&lt;br /&gt;
end&lt;br /&gt;
def &amp;lt;=&amp;gt;(other) &lt;br /&gt;
    self.length_squared &amp;lt;=&amp;gt; other.length_squared &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;=&amp;gt; returns 1,0, or –1, depending on  whether the first argument is &lt;br /&gt;
greater than, equal to, or less than the second argument.  &lt;br /&gt;
We delegate the call to &amp;lt;=&amp;gt; of the Fixnum class, which compares &lt;br /&gt;
the squares of the lengths. &lt;br /&gt;
Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
l1 = Line.new(1, 0, 4, 3) &lt;br /&gt;
l2 = Line.new(0, 0, 10, 10) &lt;br /&gt;
puts l1.length_squared &lt;br /&gt;
if l1 &amp;lt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is shorter than Line 2&amp;quot; &lt;br /&gt;
else if l1 &amp;gt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is longer than Line 2&amp;quot; &lt;br /&gt;
else &lt;br /&gt;
  puts &amp;quot;Line 1 is just as long as Line 2&amp;quot; &lt;br /&gt;
 end &lt;br /&gt;
end &lt;br /&gt;
&amp;gt;&amp;gt;Line 1 is shorter than Line 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
Enumerable is a standard mixin which can be used to put into other classes.It has a method called inject that can be applied to adjacent elements of a set.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[1, 2, 3, 4, 5].inject {|v, n| v+n }&lt;br /&gt;
&amp;gt;&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen in the above example inject is used on all elements of the set to add them and display the result.&lt;br /&gt;
&lt;br /&gt;
To understand the working of Enumerable mixin in class,consider the following example. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class VowelFinder &lt;br /&gt;
  include Enumerable &lt;br /&gt;
  def initialize(string) &lt;br /&gt;
    @string = string &lt;br /&gt;
  end &lt;br /&gt;
  def each &lt;br /&gt;
    @string.scan(/[aeiou]/) do |vowel| &lt;br /&gt;
      yield vowel &lt;br /&gt;
    end &lt;br /&gt;
  end &lt;br /&gt;
end  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
VowelFinder.new(&amp;quot;abacadabra&amp;quot;).inject {|v, n| v+n} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] has several disadvantages that can lead to ambiguous code behavior either during [http://en.wikipedia.org/wiki/Compile_time compile time] or [http://en.wikipedia.org/wiki/Run_time run time]. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules]. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
      puts &amp;quot;Inside a1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
      puts &amp;quot;Inside a2&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
       puts &amp;quot;Inside b1&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
       puts &amp;quot;Inside b2&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
      puts &amp;quot;Inside s1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1             # =&amp;gt; Inside a1&lt;br /&gt;
  obj.a2             # =&amp;gt; Inside a2&lt;br /&gt;
  obj.b1             # =&amp;gt; Inside b1&lt;br /&gt;
  obj.b2             # =&amp;gt; Inside b2&lt;br /&gt;
  obj.s1             # =&amp;gt; Inside s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses super-classes].&amp;lt;br&amp;gt;&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super-classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses sub-class].&amp;lt;br&amp;gt; &lt;br /&gt;
For example, [http://en.wikipedia.org/wiki/Mixin Mixins] help achieve this.&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by [http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html implementing the interfaces]. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase [http://en.wikipedia.org/wiki/Reusability reusability] and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
Some people call this situation the '''&amp;quot;[http://en.wikipedia.org/wiki/Diamond_problem Deadly Diamond of Death]&amp;quot;'''. This is illustrated by the Java code example below:&lt;br /&gt;
&lt;br /&gt;
  // This is our top most abstract class.&lt;br /&gt;
  class AbstractSuperClass{&lt;br /&gt;
    abstract void method();&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // These are the two concrete sub classes which extend the above super class&lt;br /&gt;
  class ConcreteSubOne extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I am going to test multiple Inheritance&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  class ConcreteSubTwo extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I will cause the Deadly Diamond of Death&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  // This is our last class which extends both of the above concrete classes&lt;br /&gt;
  class DeadlyDiamondEffect extends ConcreteSubOne, ConcreteSubTwo{&lt;br /&gt;
    //Some methods of this class&lt;br /&gt;
    //This inherits a do() method from ConcreteSubOne and another do() from ConcreteSubTwo. When this is called there is an ambiguity.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a [http://en.wikipedia.org/wiki/Object_composition object composition] but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;[http://en.wikipedia.org/wiki/Is-a Is-a]&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming renaming].&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://strugglingwithruby.blogspot.com/2009/05/regular-expressions-in-ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://javacodeonline.blogspot.com/2009/08/deadly-diamond-of-death.html Deadly Diamond of Death]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem&lt;br /&gt;
[http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses Subclasses and Superclasses]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&amp;lt;br&amp;gt;&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &amp;lt;br&amp;gt;&lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.java2s.com/Code/Ruby/Class/Rubytermsmultipleinheritanceamixin Ruby Multiple Inheritance with modules and mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &amp;lt;br&amp;gt;&lt;br /&gt;
http://rubyonrails.org/ &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53075</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53075"/>
		<updated>2011-10-20T16:31:24Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] was built as a better [http://en.wikipedia.org/wiki/Perl Perl] hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
&amp;gt;&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Character&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| []&lt;br /&gt;
| range specificication (e.g., [a-z] means a letter in the range a to z)&lt;br /&gt;
|-&lt;br /&gt;
| \w &lt;br /&gt;
| word character; same as [0-9A-Za-z_]&lt;br /&gt;
|-&lt;br /&gt;
| \W&lt;br /&gt;
| non-word character&lt;br /&gt;
|-&lt;br /&gt;
| \s&lt;br /&gt;
| space character; same as [ \t\n\r\f]&lt;br /&gt;
|-&lt;br /&gt;
| \S&lt;br /&gt;
| non-space character&lt;br /&gt;
|-&lt;br /&gt;
| \d&lt;br /&gt;
| digit character; same as [0-9]&lt;br /&gt;
|-&lt;br /&gt;
| \D&lt;br /&gt;
| non-digit character&lt;br /&gt;
|-&lt;br /&gt;
| \b&lt;br /&gt;
| backspace (0x08) (only if in a range specification)&lt;br /&gt;
|-&lt;br /&gt;
| \B&lt;br /&gt;
| non-word boundary&lt;br /&gt;
|-&lt;br /&gt;
|{m,n}	 &lt;br /&gt;
|at least m and at most n repetitions of the preceding &lt;br /&gt;
|&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++], letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
Consider the following example to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
The comparable mixin is used by classes whose objects may be ordered.The 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. if the other object is not comparable then is should just return nil.Comparable uses &amp;lt;=&amp;gt; to implement the comparison operators(&amp;lt;,&amp;lt;=,==,=&amp;gt;,and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
Here is an example to illustrate comparable mixin.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line &lt;br /&gt;
  def initialize(x1, y1, x2, y2) &lt;br /&gt;
    @x1, @y1, @x2, @y2 = x1, y1, x2, y2 &lt;br /&gt;
  end   &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We compare two lines on the basis of their lengths.&lt;br /&gt;
We add the Comparable mixin as follows: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line  &lt;br /&gt;
  include Comparable  &lt;br /&gt;
  def length_squared &lt;br /&gt;
    (@x2-@x1) * (@x2-@x1) + (@y2-@y1) * (@y2-@y1)&lt;br /&gt;
end&lt;br /&gt;
def &amp;lt;=&amp;gt;(other) &lt;br /&gt;
    self.length_squared &amp;lt;=&amp;gt; other.length_squared &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;=&amp;gt; returns 1,0, or –1, depending on  whether the first argument is &lt;br /&gt;
greater than, equal to, or less than the second argument.  &lt;br /&gt;
We delegate the call to &amp;lt;=&amp;gt; of the Fixnum class, which compares &lt;br /&gt;
the squares of the lengths. &lt;br /&gt;
Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
l1 = Line.new(1, 0, 4, 3) &lt;br /&gt;
l2 = Line.new(0, 0, 10, 10) &lt;br /&gt;
puts l1.length_squared &lt;br /&gt;
if l1 &amp;lt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is shorter than Line 2&amp;quot; &lt;br /&gt;
else if l1 &amp;gt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is longer than Line 2&amp;quot; &lt;br /&gt;
else &lt;br /&gt;
  puts &amp;quot;Line 1 is just as long as Line 2&amp;quot; &lt;br /&gt;
 end &lt;br /&gt;
end &lt;br /&gt;
&amp;gt;&amp;gt;Line 1 is shorter than Line 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
Enumerable is a standard mixin which can be used to put into other classes.It has a method called inject that can be applied to adjacent elements of a set.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[1, 2, 3, 4, 5].inject {|v, n| v+n }&lt;br /&gt;
&amp;gt;&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen in the above example inject is used on all elements of the set to add them and display the result.&lt;br /&gt;
&lt;br /&gt;
To understand the working of Enumerable mixin in class,consider the following example. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class VowelFinder &lt;br /&gt;
  include Enumerable &lt;br /&gt;
  def initialize(string) &lt;br /&gt;
    @string = string &lt;br /&gt;
  end &lt;br /&gt;
  def each &lt;br /&gt;
    @string.scan(/[aeiou]/) do |vowel| &lt;br /&gt;
      yield vowel &lt;br /&gt;
    end &lt;br /&gt;
  end &lt;br /&gt;
end  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
VowelFinder.new(&amp;quot;abacadabra&amp;quot;).inject {|v, n| v+n} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] has several disadvantages that can lead to ambiguous code behavior either during [http://en.wikipedia.org/wiki/Compile_time compile time] or [http://en.wikipedia.org/wiki/Run_time run time]. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules]. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
      puts &amp;quot;Inside a1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
      puts &amp;quot;Inside a2&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
       puts &amp;quot;Inside b1&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
       puts &amp;quot;Inside b2&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
      puts &amp;quot;Inside s1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1             # =&amp;gt; Inside a1&lt;br /&gt;
  obj.a2             # =&amp;gt; Inside a2&lt;br /&gt;
  obj.b1             # =&amp;gt; Inside b1&lt;br /&gt;
  obj.b2             # =&amp;gt; Inside b2&lt;br /&gt;
  obj.s1             # =&amp;gt; Inside s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses super-classes].&amp;lt;br&amp;gt;&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super-classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses sub-class].&amp;lt;br&amp;gt; &lt;br /&gt;
For example, [http://en.wikipedia.org/wiki/Mixin Mixins] help achieve this.&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by [http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html implementing the interfaces]. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase [http://en.wikipedia.org/wiki/Reusability reusability] and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
Some people call this situation the '''&amp;quot;[http://en.wikipedia.org/wiki/Diamond_problem Deadly Diamond of Death]&amp;quot;'''. This is illustrated by the Java code example below:&lt;br /&gt;
&lt;br /&gt;
  // This is our top most abstract class.&lt;br /&gt;
  class AbstractSuperClass{&lt;br /&gt;
    abstract void method();&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // These are the two concrete sub classes which extend the above super class&lt;br /&gt;
  class ConcreteSubOne extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I am going to test multiple Inheritance&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  class ConcreteSubTwo extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I will cause the Deadly Diamond of Death&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  // This is our last class which extends both of the above concrete classes&lt;br /&gt;
  class DeadlyDiamondEffect extends ConcreteSubOne, ConcreteSubTwo{&lt;br /&gt;
    //Some methods of this class&lt;br /&gt;
    //This inherits a do() method from ConcreteSubOne and another do() from ConcreteSubTwo. When this is called there is an ambiguity.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a [http://en.wikipedia.org/wiki/Object_composition object composition] but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;[http://en.wikipedia.org/wiki/Is-a Is-a]&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming renaming].&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://strugglingwithruby.blogspot.com/2009/05/regular-expressions-in-ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://javacodeonline.blogspot.com/2009/08/deadly-diamond-of-death.html Deadly Diamond of Death]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem&lt;br /&gt;
[http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses Subclasses and Superclasses]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&amp;lt;br&amp;gt;&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &amp;lt;br&amp;gt;&lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.java2s.com/Code/Ruby/Class/Rubytermsmultipleinheritanceamixin Ruby Multiple Inheritance with modules and mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &amp;lt;br&amp;gt;&lt;br /&gt;
http://rubyonrails.org/ &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53074</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53074"/>
		<updated>2011-10-20T16:29:43Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Regular Expressions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] was built as a better [http://en.wikipedia.org/wiki/Perl Perl] hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
&amp;gt;&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Character&lt;br /&gt;
! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| []&lt;br /&gt;
| range specificication (e.g., [a-z] means a letter in the range a to z)&lt;br /&gt;
|-&lt;br /&gt;
| \w &lt;br /&gt;
| word character; same as [0-9A-Za-z_]&lt;br /&gt;
|-&lt;br /&gt;
| \W&lt;br /&gt;
| non-word character&lt;br /&gt;
|-&lt;br /&gt;
| \s&lt;br /&gt;
| space character; same as [ \t\n\r\f]&lt;br /&gt;
|-&lt;br /&gt;
| \S&lt;br /&gt;
| non-space character&lt;br /&gt;
|-&lt;br /&gt;
| \d&lt;br /&gt;
| digit character; same as [0-9]&lt;br /&gt;
|-&lt;br /&gt;
| \D&lt;br /&gt;
| non-digit character&lt;br /&gt;
|-&lt;br /&gt;
| \b&lt;br /&gt;
| backspace (0x08) (only if in a range specification)&lt;br /&gt;
|-&lt;br /&gt;
| \B&lt;br /&gt;
| non-word boundary&lt;br /&gt;
|-&lt;br /&gt;
|{m,n}	 &lt;br /&gt;
|at least m and at most n repetitions of the preceding &lt;br /&gt;
|&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in C++, letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
Consider the following example to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
The comparable mixin is used by classes whose objects may be ordered.The 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. if the other object is not comparable then is should just return nil.Comparable uses &amp;lt;=&amp;gt; to implement the comparison operators(&amp;lt;,&amp;lt;=,==,=&amp;gt;,and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
Here is an example to illustrate comparable mixin.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line &lt;br /&gt;
  def initialize(x1, y1, x2, y2) &lt;br /&gt;
    @x1, @y1, @x2, @y2 = x1, y1, x2, y2 &lt;br /&gt;
  end   &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We compare two lines on the basis of their lengths.&lt;br /&gt;
We add the Comparable mixin as follows: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line  &lt;br /&gt;
  include Comparable  &lt;br /&gt;
  def length_squared &lt;br /&gt;
    (@x2-@x1) * (@x2-@x1) + (@y2-@y1) * (@y2-@y1)&lt;br /&gt;
end&lt;br /&gt;
def &amp;lt;=&amp;gt;(other) &lt;br /&gt;
    self.length_squared &amp;lt;=&amp;gt; other.length_squared &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;=&amp;gt; returns 1,0, or –1, depending on  whether the first argument is &lt;br /&gt;
greater than, equal to, or less than the second argument.  &lt;br /&gt;
We delegate the call to &amp;lt;=&amp;gt; of the Fixnum class, which compares &lt;br /&gt;
the squares of the lengths. &lt;br /&gt;
Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
l1 = Line.new(1, 0, 4, 3) &lt;br /&gt;
l2 = Line.new(0, 0, 10, 10) &lt;br /&gt;
puts l1.length_squared &lt;br /&gt;
if l1 &amp;lt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is shorter than Line 2&amp;quot; &lt;br /&gt;
else if l1 &amp;gt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is longer than Line 2&amp;quot; &lt;br /&gt;
else &lt;br /&gt;
  puts &amp;quot;Line 1 is just as long as Line 2&amp;quot; &lt;br /&gt;
 end &lt;br /&gt;
end &lt;br /&gt;
&amp;gt;&amp;gt;Line 1 is shorter than Line 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
Enumerable is a standard mixin which can be used to put into other classes.It has a method called inject that can be applied to adjacent elements of a set.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[1, 2, 3, 4, 5].inject {|v, n| v+n }&lt;br /&gt;
&amp;gt;&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen in the above example inject is used on all elements of the set to add them and display the result.&lt;br /&gt;
&lt;br /&gt;
To understand the working of Enumerable mixin in class,consider the following example. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class VowelFinder &lt;br /&gt;
  include Enumerable &lt;br /&gt;
  def initialize(string) &lt;br /&gt;
    @string = string &lt;br /&gt;
  end &lt;br /&gt;
  def each &lt;br /&gt;
    @string.scan(/[aeiou]/) do |vowel| &lt;br /&gt;
      yield vowel &lt;br /&gt;
    end &lt;br /&gt;
  end &lt;br /&gt;
end  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
VowelFinder.new(&amp;quot;abacadabra&amp;quot;).inject {|v, n| v+n} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] has several disadvantages that can lead to ambiguous code behavior either during [http://en.wikipedia.org/wiki/Compile_time compile time] or [http://en.wikipedia.org/wiki/Run_time run time]. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules]. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
      puts &amp;quot;Inside a1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
      puts &amp;quot;Inside a2&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
       puts &amp;quot;Inside b1&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
       puts &amp;quot;Inside b2&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
      puts &amp;quot;Inside s1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1             # =&amp;gt; Inside a1&lt;br /&gt;
  obj.a2             # =&amp;gt; Inside a2&lt;br /&gt;
  obj.b1             # =&amp;gt; Inside b1&lt;br /&gt;
  obj.b2             # =&amp;gt; Inside b2&lt;br /&gt;
  obj.s1             # =&amp;gt; Inside s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses super-classes].&amp;lt;br&amp;gt;&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super-classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses sub-class].&amp;lt;br&amp;gt; &lt;br /&gt;
For example, [http://en.wikipedia.org/wiki/Mixin Mixins] help achieve this.&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by [http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html implementing the interfaces]. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase [http://en.wikipedia.org/wiki/Reusability reusability] and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
Some people call this situation the '''&amp;quot;[http://en.wikipedia.org/wiki/Diamond_problem Deadly Diamond of Death]&amp;quot;'''. This is illustrated by the Java code example below:&lt;br /&gt;
&lt;br /&gt;
  // This is our top most abstract class.&lt;br /&gt;
  class AbstractSuperClass{&lt;br /&gt;
    abstract void method();&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // These are the two concrete sub classes which extend the above super class&lt;br /&gt;
  class ConcreteSubOne extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I am going to test multiple Inheritance&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  class ConcreteSubTwo extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I will cause the Deadly Diamond of Death&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  // This is our last class which extends both of the above concrete classes&lt;br /&gt;
  class DeadlyDiamondEffect extends ConcreteSubOne, ConcreteSubTwo{&lt;br /&gt;
    //Some methods of this class&lt;br /&gt;
    //This inherits a do() method from ConcreteSubOne and another do() from ConcreteSubTwo. When this is called there is an ambiguity.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a [http://en.wikipedia.org/wiki/Object_composition object composition] but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;[http://en.wikipedia.org/wiki/Is-a Is-a]&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming renaming].&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://strugglingwithruby.blogspot.com/2009/05/regular-expressions-in-ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://javacodeonline.blogspot.com/2009/08/deadly-diamond-of-death.html Deadly Diamond of Death]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem&lt;br /&gt;
[http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses Subclasses and Superclasses]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&amp;lt;br&amp;gt;&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &amp;lt;br&amp;gt;&lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.java2s.com/Code/Ruby/Class/Rubytermsmultipleinheritanceamixin Ruby Multiple Inheritance with modules and mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &amp;lt;br&amp;gt;&lt;br /&gt;
http://rubyonrails.org/ &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53073</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53073"/>
		<updated>2011-10-20T16:18:57Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] was built as a better [http://en.wikipedia.org/wiki/Perl Perl] hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
&amp;gt;&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in C++, letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
Consider the following example to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
The comparable mixin is used by classes whose objects may be ordered.The 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. if the other object is not comparable then is should just return nil.Comparable uses &amp;lt;=&amp;gt; to implement the comparison operators(&amp;lt;,&amp;lt;=,==,=&amp;gt;,and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
Here is an example to illustrate comparable mixin.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line &lt;br /&gt;
  def initialize(x1, y1, x2, y2) &lt;br /&gt;
    @x1, @y1, @x2, @y2 = x1, y1, x2, y2 &lt;br /&gt;
  end   &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We compare two lines on the basis of their lengths.&lt;br /&gt;
We add the Comparable mixin as follows: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line  &lt;br /&gt;
  include Comparable  &lt;br /&gt;
  def length_squared &lt;br /&gt;
    (@x2-@x1) * (@x2-@x1) + (@y2-@y1) * (@y2-@y1)&lt;br /&gt;
end&lt;br /&gt;
def &amp;lt;=&amp;gt;(other) &lt;br /&gt;
    self.length_squared &amp;lt;=&amp;gt; other.length_squared &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;=&amp;gt; returns 1,0, or –1, depending on  whether the first argument is &lt;br /&gt;
greater than, equal to, or less than the second argument.  &lt;br /&gt;
We delegate the call to &amp;lt;=&amp;gt; of the Fixnum class, which compares &lt;br /&gt;
the squares of the lengths. &lt;br /&gt;
Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
l1 = Line.new(1, 0, 4, 3) &lt;br /&gt;
l2 = Line.new(0, 0, 10, 10) &lt;br /&gt;
puts l1.length_squared &lt;br /&gt;
if l1 &amp;lt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is shorter than Line 2&amp;quot; &lt;br /&gt;
else if l1 &amp;gt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is longer than Line 2&amp;quot; &lt;br /&gt;
else &lt;br /&gt;
  puts &amp;quot;Line 1 is just as long as Line 2&amp;quot; &lt;br /&gt;
 end &lt;br /&gt;
end &lt;br /&gt;
&amp;gt;&amp;gt;Line 1 is shorter than Line 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
Enumerable is a standard mixin which can be used to put into other classes.It has a method called inject that can be applied to adjacent elements of a set.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[1, 2, 3, 4, 5].inject {|v, n| v+n }&lt;br /&gt;
&amp;gt;&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen in the above example inject is used on all elements of the set to add them and display the result.&lt;br /&gt;
&lt;br /&gt;
To understand the working of Enumerable mixin in class,consider the following example. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class VowelFinder &lt;br /&gt;
  include Enumerable &lt;br /&gt;
  def initialize(string) &lt;br /&gt;
    @string = string &lt;br /&gt;
  end &lt;br /&gt;
  def each &lt;br /&gt;
    @string.scan(/[aeiou]/) do |vowel| &lt;br /&gt;
      yield vowel &lt;br /&gt;
    end &lt;br /&gt;
  end &lt;br /&gt;
end  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
VowelFinder.new(&amp;quot;abacadabra&amp;quot;).inject {|v, n| v+n} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] has several disadvantages that can lead to ambiguous code behavior either during [http://en.wikipedia.org/wiki/Compile_time compile time] or [http://en.wikipedia.org/wiki/Run_time run time]. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules]. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
      puts &amp;quot;Inside a1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
      puts &amp;quot;Inside a2&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
       puts &amp;quot;Inside b1&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
       puts &amp;quot;Inside b2&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
      puts &amp;quot;Inside s1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1             # =&amp;gt; Inside a1&lt;br /&gt;
  obj.a2             # =&amp;gt; Inside a2&lt;br /&gt;
  obj.b1             # =&amp;gt; Inside b1&lt;br /&gt;
  obj.b2             # =&amp;gt; Inside b2&lt;br /&gt;
  obj.s1             # =&amp;gt; Inside s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses super-classes].&amp;lt;br&amp;gt;&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super-classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses sub-class].&amp;lt;br&amp;gt; &lt;br /&gt;
For example, [http://en.wikipedia.org/wiki/Mixin Mixins] help achieve this.&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by [http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html implementing the interfaces]. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase [http://en.wikipedia.org/wiki/Reusability reusability] and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
Some people call this situation the '''&amp;quot;[http://en.wikipedia.org/wiki/Diamond_problem Deadly Diamond of Death]&amp;quot;'''. This is illustrated by the Java code example below:&lt;br /&gt;
&lt;br /&gt;
  // This is our top most abstract class.&lt;br /&gt;
  class AbstractSuperClass{&lt;br /&gt;
    abstract void method();&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // These are the two concrete sub classes which extend the above super class&lt;br /&gt;
  class ConcreteSubOne extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I am going to test multiple Inheritance&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  class ConcreteSubTwo extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I will cause the Deadly Diamond of Death&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  // This is our last class which extends both of the above concrete classes&lt;br /&gt;
  class DeadlyDiamondEffect extends ConcreteSubOne, ConcreteSubTwo{&lt;br /&gt;
    //Some methods of this class&lt;br /&gt;
    //This inherits a do() method from ConcreteSubOne and another do() from ConcreteSubTwo. When this is called there is an ambiguity.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a [http://en.wikipedia.org/wiki/Object_composition object composition] but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;[http://en.wikipedia.org/wiki/Is-a Is-a]&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming renaming].&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://strugglingwithruby.blogspot.com/2009/05/regular-expressions-in-ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://javacodeonline.blogspot.com/2009/08/deadly-diamond-of-death.html Deadly Diamond of Death]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem&lt;br /&gt;
[http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses Subclasses and Superclasses]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&amp;lt;br&amp;gt;&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &amp;lt;br&amp;gt;&lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.java2s.com/Code/Ruby/Class/Rubytermsmultipleinheritanceamixin Ruby Multiple Inheritance with modules and mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &amp;lt;br&amp;gt;&lt;br /&gt;
http://rubyonrails.org/ &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53072</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53072"/>
		<updated>2011-10-20T16:18:02Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] was built as a better [http://en.wikipedia.org/wiki/Perl Perl] hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
&amp;gt;&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in C++, letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
Consider the following example to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
The comparable mixin is used by classes whose objects may be ordered.The 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. if the other object is not comparable then is should just return nil.Comparable uses &amp;lt;=&amp;gt; to implement the comparison operators(&amp;lt;,&amp;lt;=,==,=&amp;gt;,and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
Here is an example to illustrate comparable mixin.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line &lt;br /&gt;
  def initialize(x1, y1, x2, y2) &lt;br /&gt;
    @x1, @y1, @x2, @y2 = x1, y1, x2, y2 &lt;br /&gt;
  end   &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We compare two lines on the basis of their lengths.&lt;br /&gt;
We add the Comparable mixin as follows: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line  &lt;br /&gt;
  include Comparable  &lt;br /&gt;
  def length_squared &lt;br /&gt;
    (@x2-@x1) * (@x2-@x1) + (@y2-@y1) * (@y2-@y1)&lt;br /&gt;
end&lt;br /&gt;
def &amp;lt;=&amp;gt;(other) &lt;br /&gt;
    self.length_squared &amp;lt;=&amp;gt; other.length_squared &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;=&amp;gt; returns 1,0, or –1, depending on  whether the first argument is &lt;br /&gt;
greater than, equal to, or less than the second argument.  &lt;br /&gt;
We delegate the call to &amp;lt;=&amp;gt; of the Fixnum class, which compares &lt;br /&gt;
the squares of the lengths. &lt;br /&gt;
Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
l1 = Line.new(1, 0, 4, 3) &lt;br /&gt;
l2 = Line.new(0, 0, 10, 10) &lt;br /&gt;
puts l1.length_squared &lt;br /&gt;
if l1 &amp;lt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is shorter than Line 2&amp;quot; &lt;br /&gt;
else if l1 &amp;gt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is longer than Line 2&amp;quot; &lt;br /&gt;
else &lt;br /&gt;
  puts &amp;quot;Line 1 is just as long as Line 2&amp;quot; &lt;br /&gt;
 end &lt;br /&gt;
end &lt;br /&gt;
&amp;gt;&amp;gt;Line 1 is shorter than Line 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
Enumerable is a standard mixin which can be used to put into other classes.It has a method called inject that can be applied to adjacent elements of a set.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[1, 2, 3, 4, 5].inject {|v, n| v+n }&lt;br /&gt;
&amp;gt;&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen in the above example inject is used on all elements of the set to add them and display the result.&lt;br /&gt;
&lt;br /&gt;
To understand the working of Enumerable mixin in class,consider the following example. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class VowelFinder &lt;br /&gt;
  include Enumerable &lt;br /&gt;
  def initialize(string) &lt;br /&gt;
    @string = string &lt;br /&gt;
  end &lt;br /&gt;
  def each &lt;br /&gt;
    @string.scan(/[aeiou]/) do |vowel| &lt;br /&gt;
      yield vowel &lt;br /&gt;
    end &lt;br /&gt;
  end &lt;br /&gt;
end  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
VowelFinder.new(&amp;quot;abacadabra&amp;quot;).inject {|v, n| v+n} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] has several disadvantages that can lead to ambiguous code behavior either during [http://en.wikipedia.org/wiki/Compile_time compile time] or [http://en.wikipedia.org/wiki/Run_time run time]. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules]. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
      puts &amp;quot;Inside a1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
      puts &amp;quot;Inside a2&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
       puts &amp;quot;Inside b1&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
       puts &amp;quot;Inside b2&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
      puts &amp;quot;Inside s1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1             # =&amp;gt; Inside a1&lt;br /&gt;
  obj.a2             # =&amp;gt; Inside a2&lt;br /&gt;
  obj.b1             # =&amp;gt; Inside b1&lt;br /&gt;
  obj.b2             # =&amp;gt; Inside b2&lt;br /&gt;
  obj.s1             # =&amp;gt; Inside s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses super-classes].&amp;lt;br&amp;gt;&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super-classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses sub-class].&amp;lt;br&amp;gt; &lt;br /&gt;
For example, [http://en.wikipedia.org/wiki/Mixin Mixins] help achieve this.&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by [http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html implementing the interfaces]. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase [http://en.wikipedia.org/wiki/Reusability reusability] and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
Some people call this situation the '''&amp;quot;[http://en.wikipedia.org/wiki/Diamond_problem Deadly Diamond of Death]&amp;quot;'''. This is illustrated by the Java code example below:&lt;br /&gt;
&lt;br /&gt;
  // This is our top most abstract class.&lt;br /&gt;
  class AbstractSuperClass{&lt;br /&gt;
    abstract void method();&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // These are the two concrete sub classes which extend the above super class&lt;br /&gt;
  class ConcreteSubOne extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I am going to test multiple Inheritance&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  class ConcreteSubTwo extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I will cause the Deadly Diamond of Death&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  // This is our last class which extends both of the above concrete classes&lt;br /&gt;
  class DeadlyDiamondEffect extends ConcreteSubOne, ConcreteSubTwo{&lt;br /&gt;
    //Some methods of this class&lt;br /&gt;
    //This inherits a do() method from ConcreteSubOne and another do() from ConcreteSubTwo. When this is called there is an ambiguity.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a [http://en.wikipedia.org/wiki/Object_composition object composition] but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;[http://en.wikipedia.org/wiki/Is-a Is-a]&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming renaming].&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.regular-expressions.info/ruby.html]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://javacodeonline.blogspot.com/2009/08/deadly-diamond-of-death.html Deadly Diamond of Death]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem&lt;br /&gt;
[http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses Subclasses and Superclasses]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&amp;lt;br&amp;gt;&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &amp;lt;br&amp;gt;&lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.java2s.com/Code/Ruby/Class/Rubytermsmultipleinheritanceamixin Ruby Multiple Inheritance with modules and mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &amp;lt;br&amp;gt;&lt;br /&gt;
http://rubyonrails.org/ &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53070</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53070"/>
		<updated>2011-10-20T16:10:25Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Regular Expressions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] was built as a better [http://en.wikipedia.org/wiki/Perl Perl] hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
&amp;gt;&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in C++, letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
Consider the following example to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
The comparable mixin is used by classes whose objects may be ordered.The 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. if the other object is not comparable then is should just return nil.Comparable uses &amp;lt;=&amp;gt; to implement the comparison operators(&amp;lt;,&amp;lt;=,==,=&amp;gt;,and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
Here is an example to illustrate comparable mixin.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line &lt;br /&gt;
  def initialize(x1, y1, x2, y2) &lt;br /&gt;
    @x1, @y1, @x2, @y2 = x1, y1, x2, y2 &lt;br /&gt;
  end   &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We compare two lines on the basis of their lengths.&lt;br /&gt;
We add the Comparable mixin as follows: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line  &lt;br /&gt;
  include Comparable  &lt;br /&gt;
  def length_squared &lt;br /&gt;
    (@x2-@x1) * (@x2-@x1) + (@y2-@y1) * (@y2-@y1)&lt;br /&gt;
end&lt;br /&gt;
def &amp;lt;=&amp;gt;(other) &lt;br /&gt;
    self.length_squared &amp;lt;=&amp;gt; other.length_squared &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;=&amp;gt; returns 1,0, or –1, depending on  whether the first argument is &lt;br /&gt;
greater than, equal to, or less than the second argument.  &lt;br /&gt;
We delegate the call to &amp;lt;=&amp;gt; of the Fixnum class, which compares &lt;br /&gt;
the squares of the lengths. &lt;br /&gt;
Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
l1 = Line.new(1, 0, 4, 3) &lt;br /&gt;
l2 = Line.new(0, 0, 10, 10) &lt;br /&gt;
puts l1.length_squared &lt;br /&gt;
if l1 &amp;lt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is shorter than Line 2&amp;quot; &lt;br /&gt;
else if l1 &amp;gt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is longer than Line 2&amp;quot; &lt;br /&gt;
else &lt;br /&gt;
  puts &amp;quot;Line 1 is just as long as Line 2&amp;quot; &lt;br /&gt;
 end &lt;br /&gt;
end &lt;br /&gt;
&amp;gt;&amp;gt;Line 1 is shorter than Line 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
Enumerable is a standard mixin which can be used to put into other classes.It has a method called inject that can be applied to adjacent elements of a set.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[1, 2, 3, 4, 5].inject {|v, n| v+n }&lt;br /&gt;
&amp;gt;&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen in the above example inject is used on all elements of the set to add them and display the result.&lt;br /&gt;
&lt;br /&gt;
To understand the working of Enumerable mixin in class,consider the following example. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class VowelFinder &lt;br /&gt;
  include Enumerable &lt;br /&gt;
  def initialize(string) &lt;br /&gt;
    @string = string &lt;br /&gt;
  end &lt;br /&gt;
  def each &lt;br /&gt;
    @string.scan(/[aeiou]/) do |vowel| &lt;br /&gt;
      yield vowel &lt;br /&gt;
    end &lt;br /&gt;
  end &lt;br /&gt;
end  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
VowelFinder.new(&amp;quot;abacadabra&amp;quot;).inject {|v, n| v+n} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] has several disadvantages that can lead to ambiguous code behavior either during [http://en.wikipedia.org/wiki/Compile_time compile time] or [http://en.wikipedia.org/wiki/Run_time run time]. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules]. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
      puts &amp;quot;Inside a1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
      puts &amp;quot;Inside a2&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
       puts &amp;quot;Inside b1&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
       puts &amp;quot;Inside b2&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
      puts &amp;quot;Inside s1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1             # =&amp;gt; Inside a1&lt;br /&gt;
  obj.a2             # =&amp;gt; Inside a2&lt;br /&gt;
  obj.b1             # =&amp;gt; Inside b1&lt;br /&gt;
  obj.b2             # =&amp;gt; Inside b2&lt;br /&gt;
  obj.s1             # =&amp;gt; Inside s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses super-classes].&amp;lt;br&amp;gt;&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super-classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses sub-class].&amp;lt;br&amp;gt; &lt;br /&gt;
For example, [http://en.wikipedia.org/wiki/Mixin Mixins] help achieve this.&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by [http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html implementing the interfaces]. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase [http://en.wikipedia.org/wiki/Reusability reusability] and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
Some people call this situation the '''&amp;quot;[http://en.wikipedia.org/wiki/Diamond_problem Deadly Diamond of Death]&amp;quot;'''. This is illustrated by the Java code example below:&lt;br /&gt;
&lt;br /&gt;
  // This is our top most abstract class.&lt;br /&gt;
  class AbstractSuperClass{&lt;br /&gt;
    abstract void method();&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // These are the two concrete sub classes which extend the above super class&lt;br /&gt;
  class ConcreteSubOne extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I am going to test multiple Inheritance&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  class ConcreteSubTwo extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I will cause the Deadly Diamond of Death&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  // This is our last class which extends both of the above concrete classes&lt;br /&gt;
  class DeadlyDiamondEffect extends ConcreteSubOne, ConcreteSubTwo{&lt;br /&gt;
    //Some methods of this class&lt;br /&gt;
    //This inherits a do() method from ConcreteSubOne and another do() from ConcreteSubTwo. When this is called there is an ambiguity.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a [http://en.wikipedia.org/wiki/Object_composition object composition] but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;[http://en.wikipedia.org/wiki/Is-a Is-a]&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming renaming].&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://javacodeonline.blogspot.com/2009/08/deadly-diamond-of-death.html Deadly Diamond of Death]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem&lt;br /&gt;
[http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses Subclasses and Superclasses]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&amp;lt;br&amp;gt;&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &amp;lt;br&amp;gt;&lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.java2s.com/Code/Ruby/Class/Rubytermsmultipleinheritanceamixin Ruby Multiple Inheritance with modules and mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &amp;lt;br&amp;gt;&lt;br /&gt;
http://rubyonrails.org/ &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53068</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53068"/>
		<updated>2011-10-20T16:07:02Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Regular Expressions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] was built as a better Perl hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; &amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
=&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
irb(main):002:0&amp;gt; r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
irb(main):003:0&amp;gt; r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):014:0&amp;gt; e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
irb(main):016:0&amp;gt; string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
irb(main):017:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):020:0&amp;gt; e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
irb(main):021:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in C++, letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
Consider the following example to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
The comparable mixin is used by classes whose objects may be ordered.The 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. if the other object is not comparable then is should just return nil.Comparable uses &amp;lt;=&amp;gt; to implement the comparison operators(&amp;lt;,&amp;lt;=,==,=&amp;gt;,and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
Here is an example to illustrate comparable mixin.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line &lt;br /&gt;
  def initialize(x1, y1, x2, y2) &lt;br /&gt;
    @x1, @y1, @x2, @y2 = x1, y1, x2, y2 &lt;br /&gt;
  end   &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We compare two lines on the basis of their lengths.&lt;br /&gt;
We add the Comparable mixin as follows: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line  &lt;br /&gt;
  include Comparable  &lt;br /&gt;
  def length_squared &lt;br /&gt;
    (@x2-@x1) * (@x2-@x1) + (@y2-@y1) * (@y2-@y1)&lt;br /&gt;
end&lt;br /&gt;
def &amp;lt;=&amp;gt;(other) &lt;br /&gt;
    self.length_squared &amp;lt;=&amp;gt; other.length_squared &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;=&amp;gt; returns 1,0, or –1, depending on  whether the first argument is &lt;br /&gt;
greater than, equal to, or less than the second argument.  &lt;br /&gt;
We delegate the call to &amp;lt;=&amp;gt; of the Fixnum class, which compares &lt;br /&gt;
the squares of the lengths. &lt;br /&gt;
Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
l1 = Line.new(1, 0, 4, 3) &lt;br /&gt;
l2 = Line.new(0, 0, 10, 10) &lt;br /&gt;
puts l1.length_squared &lt;br /&gt;
if l1 &amp;lt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is shorter than Line 2&amp;quot; &lt;br /&gt;
else if l1 &amp;gt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is longer than Line 2&amp;quot; &lt;br /&gt;
else &lt;br /&gt;
  puts &amp;quot;Line 1 is just as long as Line 2&amp;quot; &lt;br /&gt;
 end &lt;br /&gt;
end &lt;br /&gt;
&amp;gt;&amp;gt;Line 1 is shorter than Line 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
Enumerable is a standard mixin which can be used to put into other classes.It has a method called inject that can be applied to adjacent elements of a set.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[1, 2, 3, 4, 5].inject {|v, n| v+n }&lt;br /&gt;
&amp;gt;&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen in the above example inject is used on all elements of the set to add them and display the result.&lt;br /&gt;
&lt;br /&gt;
To understand the working of Enumerable mixin in class,consider the following example. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class VowelFinder &lt;br /&gt;
  include Enumerable &lt;br /&gt;
  def initialize(string) &lt;br /&gt;
    @string = string &lt;br /&gt;
  end &lt;br /&gt;
  def each &lt;br /&gt;
    @string.scan(/[aeiou]/) do |vowel| &lt;br /&gt;
      yield vowel &lt;br /&gt;
    end &lt;br /&gt;
  end &lt;br /&gt;
end  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
VowelFinder.new(&amp;quot;abacadabra&amp;quot;).inject {|v, n| v+n} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] has several disadvantages that can lead to ambiguous code behavior either during [http://en.wikipedia.org/wiki/Compile_time compile time] or [http://en.wikipedia.org/wiki/Run_time run time]. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules]. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
      puts &amp;quot;Inside a1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
      puts &amp;quot;Inside a2&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
       puts &amp;quot;Inside b1&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
       puts &amp;quot;Inside b2&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
      puts &amp;quot;Inside s1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1             # =&amp;gt; Inside a1&lt;br /&gt;
  obj.a2             # =&amp;gt; Inside a2&lt;br /&gt;
  obj.b1             # =&amp;gt; Inside b1&lt;br /&gt;
  obj.b2             # =&amp;gt; Inside b2&lt;br /&gt;
  obj.s1             # =&amp;gt; Inside s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses super-classes].&amp;lt;br&amp;gt;&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super-classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses sub-class].&amp;lt;br&amp;gt; &lt;br /&gt;
For example, [http://en.wikipedia.org/wiki/Mixin Mixins] help achieve this.&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by [http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html implementing the interfaces]. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase [http://en.wikipedia.org/wiki/Reusability reusability] and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
Some people call this situation the '''&amp;quot;[http://en.wikipedia.org/wiki/Diamond_problem Deadly Diamond of Death]&amp;quot;'''. This is illustrated by the Java code example below:&lt;br /&gt;
&lt;br /&gt;
  // This is our top most abstract class.&lt;br /&gt;
  class AbstractSuperClass{&lt;br /&gt;
    abstract void method();&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // These are the two concrete sub classes which extend the above super class&lt;br /&gt;
  class ConcreteSubOne extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I am going to test multiple Inheritance&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  class ConcreteSubTwo extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I will cause the Deadly Diamond of Death&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  // This is our last class which extends both of the above concrete classes&lt;br /&gt;
  class DeadlyDiamondEffect extends ConcreteSubOne, ConcreteSubTwo{&lt;br /&gt;
    //Some methods of this class&lt;br /&gt;
    //This inherits a do() method from ConcreteSubOne and another do() from ConcreteSubTwo. When this is called there is an ambiguity.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a [http://en.wikipedia.org/wiki/Object_composition object composition] but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;[http://en.wikipedia.org/wiki/Is-a Is-a]&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming renaming].&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://javacodeonline.blogspot.com/2009/08/deadly-diamond-of-death.html Deadly Diamond of Death]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem&lt;br /&gt;
[http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses Subclasses and Superclasses]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&amp;lt;br&amp;gt;&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &amp;lt;br&amp;gt;&lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.java2s.com/Code/Ruby/Class/Rubytermsmultipleinheritanceamixin Ruby Multiple Inheritance with modules and mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &amp;lt;br&amp;gt;&lt;br /&gt;
http://rubyonrails.org/ &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53063</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=53063"/>
		<updated>2011-10-20T16:00:27Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Composing Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; &amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
=&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
irb(main):002:0&amp;gt; r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
irb(main):003:0&amp;gt; r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):014:0&amp;gt; e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
irb(main):016:0&amp;gt; string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
irb(main):017:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):020:0&amp;gt; e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
irb(main):021:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in C++, letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
Consider the following example to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
The comparable mixin is used by classes whose objects may be ordered.The 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. if the other object is not comparable then is should just return nil.Comparable uses &amp;lt;=&amp;gt; to implement the comparison operators(&amp;lt;,&amp;lt;=,==,=&amp;gt;,and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
Here is an example to illustrate comparable mixin.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line &lt;br /&gt;
  def initialize(x1, y1, x2, y2) &lt;br /&gt;
    @x1, @y1, @x2, @y2 = x1, y1, x2, y2 &lt;br /&gt;
  end   &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We compare two lines on the basis of their lengths.&lt;br /&gt;
We add the Comparable mixin as follows: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line  &lt;br /&gt;
  include Comparable  &lt;br /&gt;
  def length_squared &lt;br /&gt;
    (@x2-@x1) * (@x2-@x1) + (@y2-@y1) * (@y2-@y1)&lt;br /&gt;
end&lt;br /&gt;
def &amp;lt;=&amp;gt;(other) &lt;br /&gt;
    self.length_squared &amp;lt;=&amp;gt; other.length_squared &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;=&amp;gt; returns 1,0, or –1, depending on  whether the first argument is &lt;br /&gt;
greater than, equal to, or less than the second argument.  &lt;br /&gt;
We delegate the call to &amp;lt;=&amp;gt; of the Fixnum class, which compares &lt;br /&gt;
the squares of the lengths. &lt;br /&gt;
Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
l1 = Line.new(1, 0, 4, 3) &lt;br /&gt;
l2 = Line.new(0, 0, 10, 10) &lt;br /&gt;
puts l1.length_squared &lt;br /&gt;
if l1 &amp;lt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is shorter than Line 2&amp;quot; &lt;br /&gt;
else if l1 &amp;gt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is longer than Line 2&amp;quot; &lt;br /&gt;
else &lt;br /&gt;
  puts &amp;quot;Line 1 is just as long as Line 2&amp;quot; &lt;br /&gt;
 end &lt;br /&gt;
end &lt;br /&gt;
&amp;gt;&amp;gt;Line 1 is shorter than Line 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
Enumerable is a standard mixin which can be used to put into other classes.It has a method called inject that can be applied to adjacent elements of a set.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[1, 2, 3, 4, 5].inject {|v, n| v+n }&lt;br /&gt;
&amp;gt;&amp;gt;15&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen in the above example inject is used on all elements of the set to add them and display the result.&lt;br /&gt;
&lt;br /&gt;
To understand the working of Enumerable mixin in class,consider the following example. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class VowelFinder &lt;br /&gt;
  include Enumerable &lt;br /&gt;
  def initialize(string) &lt;br /&gt;
    @string = string &lt;br /&gt;
  end &lt;br /&gt;
  def each &lt;br /&gt;
    @string.scan(/[aeiou]/) do |vowel| &lt;br /&gt;
      yield vowel &lt;br /&gt;
    end &lt;br /&gt;
  end &lt;br /&gt;
end  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
VowelFinder.new(&amp;quot;abacadabra&amp;quot;).inject {|v, n| v+n} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] has several disadvantages that can lead to ambiguous code behavior either during [http://en.wikipedia.org/wiki/Compile_time compile time] or [http://en.wikipedia.org/wiki/Run_time run time]. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through [http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html Modules]. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
      puts &amp;quot;Inside a1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
      puts &amp;quot;Inside a2&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
       puts &amp;quot;Inside b1&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
       puts &amp;quot;Inside b2&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
      puts &amp;quot;Inside s1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1             # =&amp;gt; Inside a1&lt;br /&gt;
  obj.a2             # =&amp;gt; Inside a2&lt;br /&gt;
  obj.b1             # =&amp;gt; Inside b1&lt;br /&gt;
  obj.b2             # =&amp;gt; Inside b2&lt;br /&gt;
  obj.s1             # =&amp;gt; Inside s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses super-classes].&amp;lt;br&amp;gt;&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super-classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single [http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses sub-class].&amp;lt;br&amp;gt; &lt;br /&gt;
For example, [http://en.wikipedia.org/wiki/Mixin Mixins] help achieve this.&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interface_%28Java%29 Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by [http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html implementing the interfaces]. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase [http://en.wikipedia.org/wiki/Reusability reusability] and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
Some people call this situation the '''&amp;quot;[http://en.wikipedia.org/wiki/Diamond_problem Deadly Diamond of Death]&amp;quot;'''. This is illustrated by the Java code example below:&lt;br /&gt;
&lt;br /&gt;
  // This is our top most abstract class.&lt;br /&gt;
  class AbstractSuperClass{&lt;br /&gt;
    abstract void method();&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // These are the two concrete sub classes which extend the above super class&lt;br /&gt;
  class ConcreteSubOne extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I am going to test multiple Inheritance&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  class ConcreteSubTwo extends AbstractSuperClass{&lt;br /&gt;
    void method(){&lt;br /&gt;
      System.out.println(&amp;quot;I will cause the Deadly Diamond of Death&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  // This is our last class which extends both of the above concrete classes&lt;br /&gt;
  class DeadlyDiamondEffect extends ConcreteSubOne, ConcreteSubTwo{&lt;br /&gt;
    //Some methods of this class&lt;br /&gt;
    //This inherits a do() method from ConcreteSubOne and another do() from ConcreteSubTwo. When this is called there is an ambiguity.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a [http://en.wikipedia.org/wiki/Object_composition object composition] but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;[http://en.wikipedia.org/wiki/Is-a Is-a]&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming renaming].&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]&amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://javacodeonline.blogspot.com/2009/08/deadly-diamond-of-death.html Deadly Diamond of Death]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem&lt;br /&gt;
[http://en.wikipedia.org/wiki/Subclass_%28computer_science%29#Subclasses_and_superclasses Subclasses and Superclasses]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &amp;lt;br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&amp;lt;br&amp;gt;&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &amp;lt;br&amp;gt;&lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.java2s.com/Code/Ruby/Class/Rubytermsmultipleinheritanceamixin Ruby Multiple Inheritance with modules and mixins]&amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &amp;lt;br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &amp;lt;br&amp;gt;&lt;br /&gt;
http://rubyonrails.org/ &amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/satishtalim/tutorial.html Learn Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 Reflection]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.python.org/ Python]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.perl.org/ Perl]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubyonrails.org/ Ruby on Rails]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pragprog.com/book/ppmetr/metaprogramming-ruby Metaprogramming Ruby: Program Like the Ruby Pros]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://corelib.rubyonrails.org/classes/GC.html Garbage Collection in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Thread.html Threads in Ruby]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/ Ruby interface to C modules]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://en.wikipedia.org/wiki/Currying Currying]&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52340</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52340"/>
		<updated>2011-10-17T22:56:36Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Comparable */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; &amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
=&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
irb(main):002:0&amp;gt; r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
irb(main):003:0&amp;gt; r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):014:0&amp;gt; e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
irb(main):016:0&amp;gt; string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
irb(main):017:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):020:0&amp;gt; e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
irb(main):021:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in C++, letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
Consider the following example to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
The comparable mixin is used by classes whose objects may be ordered.The 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. if the other object is not comparable then is should just return nil.Comparable uses &amp;lt;=&amp;gt; to implement the comparison operators(&amp;lt;,&amp;lt;=,==,=&amp;gt;,and &amp;gt;) and the method between?.&lt;br /&gt;
&lt;br /&gt;
Here is an example to illustrate comparable mixin.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line &lt;br /&gt;
  def initialize(x1, y1, x2, y2) &lt;br /&gt;
    @x1, @y1, @x2, @y2 = x1, y1, x2, y2 &lt;br /&gt;
  end   &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We compare two lines on the basis of their lengths.&lt;br /&gt;
We add the Comparable mixin as follows: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Line  &lt;br /&gt;
  include Comparable  &lt;br /&gt;
  def length_squared &lt;br /&gt;
    (@x2-@x1) * (@x2-@x1) + (@y2-@y1) * (@y2-@y1)&lt;br /&gt;
end&lt;br /&gt;
def &amp;lt;=&amp;gt;(other) &lt;br /&gt;
    self.length_squared &amp;lt;=&amp;gt; other.length_squared &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;=&amp;gt; returns 1,0, or –1, depending on  whether the first argument is &lt;br /&gt;
greater than, equal to, or less than the second argument.  &lt;br /&gt;
We delegate the call to &amp;lt;=&amp;gt; of the Fixnum class, which compares &lt;br /&gt;
the squares of the lengths. &lt;br /&gt;
Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
l1 = Line.new(1, 0, 4, 3) &lt;br /&gt;
l2 = Line.new(0, 0, 10, 10) &lt;br /&gt;
puts l1.length_squared &lt;br /&gt;
if l1 &amp;lt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is shorter than Line 2&amp;quot; &lt;br /&gt;
else if l1 &amp;gt; l2 &lt;br /&gt;
  puts &amp;quot;Line 1 is longer than Line 2&amp;quot; &lt;br /&gt;
else &lt;br /&gt;
  puts &amp;quot;Line 1 is just as long as Line 2&amp;quot; &lt;br /&gt;
 end &lt;br /&gt;
end &lt;br /&gt;
&amp;gt;&amp;gt;Line 1 is shorter than Line 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Multiple Inheritance has several disadvantages that can lead to ambiguous code behavior either during compile time or run time. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
      puts &amp;quot;Inside a1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
      puts &amp;quot;Inside a2&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
       puts &amp;quot;Inside b1&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
       puts &amp;quot;Inside b2&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
      puts &amp;quot;Inside s1&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1             # =&amp;gt; Inside a1&lt;br /&gt;
  obj.a2             # =&amp;gt; Inside a2&lt;br /&gt;
  obj.b1             # =&amp;gt; Inside b1&lt;br /&gt;
  obj.b2             # =&amp;gt; Inside b2&lt;br /&gt;
  obj.s1             # =&amp;gt; Inside s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different super-classes. &lt;br /&gt;
&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single sub-class. &lt;br /&gt;
&lt;br /&gt;
For example, Mixins help achieve this.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
&lt;br /&gt;
For example, in a class representing mortgages, one might factor out FIXED_RATE and ADJUSTABLE mortgages. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing them. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase re-usability and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
In Java, some people call this situation the &amp;quot;Deadly Diamond of Death&amp;quot;. This is illustrated by the figure below:&lt;br /&gt;
&lt;br /&gt;
figure&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a composition but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Is-a&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming.&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.java2s.com/Code/Ruby/Class/Rubytermsmultipleinheritanceamixin Ruby Multiple Inheritance with modules and mixins]&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52336</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52336"/>
		<updated>2011-10-17T22:35:13Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; &amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
=&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
irb(main):002:0&amp;gt; r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
irb(main):003:0&amp;gt; r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):014:0&amp;gt; e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
irb(main):016:0&amp;gt; string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
irb(main):017:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):020:0&amp;gt; e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
irb(main):021:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in C++, letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
Consider the following example to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Multiple Inheritance has several disadvantages that can lead to ambiguous code behavior either during compile time or run time. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1&lt;br /&gt;
  obj.a2&lt;br /&gt;
  obj.b1&lt;br /&gt;
  obj.b2&lt;br /&gt;
  obj.s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different super-classes. &lt;br /&gt;
&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single sub-class. &lt;br /&gt;
&lt;br /&gt;
For example, Mixins help achieve this.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
&lt;br /&gt;
For example, in a class representing mortgages, one might factor out FIXED_RATE and ADJUSTABLE mortgages. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing them. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase re-usability and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
In Java, some people call this situation the &amp;quot;Deadly Diamond of Death&amp;quot;. This is illustrated by the figure below:&lt;br /&gt;
&lt;br /&gt;
figure&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a composition but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Is-a&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming.&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52334</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52334"/>
		<updated>2011-10-17T22:34:43Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; &amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
=&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
irb(main):002:0&amp;gt; r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
irb(main):003:0&amp;gt; r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):014:0&amp;gt; e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
irb(main):016:0&amp;gt; string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
irb(main):017:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):020:0&amp;gt; e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
irb(main):021:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in C++, letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.&lt;br /&gt;
&lt;br /&gt;
When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
Consider the following example to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Multiple Inheritance has several disadvantages that can lead to ambiguous code behavior either during compile time or run time. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given below is another simple example of how modules and mixins can be used to simulate multiple inheritance in Ruby. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  module A&lt;br /&gt;
    def a1&lt;br /&gt;
    end&lt;br /&gt;
    def a2&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  module B&lt;br /&gt;
     def b1&lt;br /&gt;
     end&lt;br /&gt;
     def b2&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class Sample&lt;br /&gt;
    include A&lt;br /&gt;
    include B&lt;br /&gt;
    def s1&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  obj = Sample.new&lt;br /&gt;
  obj.a1&lt;br /&gt;
  obj.a2&lt;br /&gt;
  obj.b1&lt;br /&gt;
  obj.b2&lt;br /&gt;
  obj.s1&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different super-classes. &lt;br /&gt;
&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single sub-class. &lt;br /&gt;
&lt;br /&gt;
For example, Mixins help achieve this.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
&lt;br /&gt;
For example, in a class representing mortgages, one might factor out FIXED_RATE and ADJUSTABLE mortgages. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing them. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase re-usability and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
In Java, some people call this situation the &amp;quot;Deadly Diamond of Death&amp;quot;. This is illustrated by the figure below:&lt;br /&gt;
&lt;br /&gt;
figure&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a composition but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Is-a&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming.&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52331</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52331"/>
		<updated>2011-10-17T22:33:47Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; &amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
=&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
irb(main):002:0&amp;gt; r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
irb(main):003:0&amp;gt; r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):014:0&amp;gt; e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
irb(main):016:0&amp;gt; string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
irb(main):017:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):020:0&amp;gt; e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
irb(main):021:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in C++, letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
The most interesting fact about the use of modules is to define mixins.&lt;br /&gt;
&lt;br /&gt;
When a module is included within a class,all its functionality becomes available to the class.Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
Let's see how mixins is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
&lt;br /&gt;
Mixins corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
Consider the following example to illustrate mixins.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following code: &lt;br /&gt;
module Introspect &lt;br /&gt;
  def kind &lt;br /&gt;
    puts &amp;quot;#{self.class.name}&amp;quot; &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Animal &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(name) &lt;br /&gt;
     @name = name &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
class Car &lt;br /&gt;
  include Introspect &lt;br /&gt;
  def initialize(model) &lt;br /&gt;
    @model = model &lt;br /&gt;
  end &lt;br /&gt;
end &lt;br /&gt;
d = Animal.new(&amp;quot;Cat&amp;quot;) &lt;br /&gt;
c = Car.new(&amp;quot;Ferrari&amp;quot;) &lt;br /&gt;
d.kind # kind method is available through … &lt;br /&gt;
c.kind # .. the mixin Introspect &lt;br /&gt;
&amp;gt;&amp;gt;Animal &lt;br /&gt;
&amp;gt;&amp;gt;Car &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Multiple Inheritance has several disadvantages that can lead to ambiguous code behavior either during compile time or run time. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different super-classes. &lt;br /&gt;
&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single sub-class. &lt;br /&gt;
&lt;br /&gt;
For example, Mixins help achieve this.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
&lt;br /&gt;
For example, in a class representing mortgages, one might factor out FIXED_RATE and ADJUSTABLE mortgages. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing them. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase re-usability and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
In Java, some people call this situation the &amp;quot;Deadly Diamond of Death&amp;quot;. This is illustrated by the figure below:&lt;br /&gt;
&lt;br /&gt;
figure&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a composition but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Is-a&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming.&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52326</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52326"/>
		<updated>2011-10-17T22:09:22Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; &amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
=&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
irb(main):002:0&amp;gt; r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
irb(main):003:0&amp;gt; r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):014:0&amp;gt; e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
irb(main):016:0&amp;gt; string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
irb(main):017:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):020:0&amp;gt; e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
irb(main):021:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in C++, letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names.&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Multiple Inheritance has several disadvantages that can lead to ambiguous code behavior either during compile time or run time. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different super-classes. &lt;br /&gt;
&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single sub-class. &lt;br /&gt;
&lt;br /&gt;
For example, Mixins help achieve this.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
&lt;br /&gt;
For example, in a class representing mortgages, one might factor out FIXED_RATE and ADJUSTABLE mortgages. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing them. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase re-usability and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
In Java, some people call this situation the &amp;quot;Deadly Diamond of Death&amp;quot;. This is illustrated by the figure below:&lt;br /&gt;
&lt;br /&gt;
figure&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a composition but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Is-a&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming.&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52325</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52325"/>
		<updated>2011-10-17T22:08:50Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; &amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
=&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
irb(main):002:0&amp;gt; r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
irb(main):003:0&amp;gt; r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):014:0&amp;gt; e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
irb(main):016:0&amp;gt; string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
irb(main):017:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):020:0&amp;gt; e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
irb(main):021:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in C++, letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example to illustrate modules.Suppose there is a graphics library that contains classes for Windows(windows.rb) and Border(border.rb).Window.rb and border.rb have a top method,which gives the position of the top of the Windows and top of the border respectively.&lt;br /&gt;
&lt;br /&gt;
To layout windows with borders,both windows.rb and border.rb have to be loaded into the program.However, as the top method is in both classes one of them will be overridden.The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the &lt;br /&gt;
two files using the Ruby require statement, and reference the &lt;br /&gt;
qualified names. &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Multiple Inheritance has several disadvantages that can lead to ambiguous code behavior either during compile time or run time. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different super-classes. &lt;br /&gt;
&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single sub-class. &lt;br /&gt;
&lt;br /&gt;
For example, Mixins help achieve this.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
&lt;br /&gt;
For example, in a class representing mortgages, one might factor out FIXED_RATE and ADJUSTABLE mortgages. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing them. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase re-usability and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
In Java, some people call this situation the &amp;quot;Deadly Diamond of Death&amp;quot;. This is illustrated by the figure below:&lt;br /&gt;
&lt;br /&gt;
figure&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a composition but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Is-a&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming.&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
[http://blog.jayfields.com/2006/05/ruby-extend-and-include.html Ruby extend and Include]&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52321</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52321"/>
		<updated>2011-10-17T21:53:19Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; &amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
=&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
irb(main):002:0&amp;gt; r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
irb(main):003:0&amp;gt; r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):014:0&amp;gt; e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
irb(main):016:0&amp;gt; string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
irb(main):017:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):020:0&amp;gt; e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
irb(main):021:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&lt;br /&gt;
&lt;br /&gt;
1.They act as namespace in C++, letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example to illustrate modules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Multiple Inheritance has several disadvantages that can lead to ambiguous code behavior either during compile time or run time. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different super-classes. &lt;br /&gt;
&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single sub-class. &lt;br /&gt;
&lt;br /&gt;
For example, Mixins help achieve this.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
&lt;br /&gt;
For example, in a class representing mortgages, one might factor out FIXED_RATE and ADJUSTABLE mortgages. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing them. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase re-usability and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
In Java, some people call this situation the &amp;quot;Deadly Diamond of Death&amp;quot;. This is illustrated by the figure below:&lt;br /&gt;
&lt;br /&gt;
figure&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a composition but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Is-a&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming.&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52318</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52318"/>
		<updated>2011-10-17T21:51:36Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; &amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
=&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
irb(main):002:0&amp;gt; r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
irb(main):003:0&amp;gt; r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):014:0&amp;gt; e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
irb(main):016:0&amp;gt; string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
irb(main):017:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):020:0&amp;gt; e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
irb(main):021:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects cannot be created based on modules nor can it be sub classed.However, it can be specified that the functionality of one module should be added to another class, or a specific object.&lt;br /&gt;
&lt;br /&gt;
Modules serve two purposes:&lt;br /&gt;
1.They act as namespace in C++, letting definition of methods whose names will not clash with those defined elsewhere.&lt;br /&gt;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
Here's an example to illustrate modules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Window &lt;br /&gt;
def Window.top &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
def Window.bottom &lt;br /&gt;
# .. &lt;br /&gt;
end &lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Border &lt;br /&gt;
def Border.top &lt;br /&gt;
# ... &lt;br /&gt;
end &lt;br /&gt;
def Border.width &lt;br /&gt;
# .. &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Multiple Inheritance has several disadvantages that can lead to ambiguous code behavior either during compile time or run time. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different super-classes. &lt;br /&gt;
&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single sub-class. &lt;br /&gt;
&lt;br /&gt;
For example, Mixins help achieve this.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
&lt;br /&gt;
For example, in a class representing mortgages, one might factor out FIXED_RATE and ADJUSTABLE mortgages. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing them. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase re-usability and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
In Java, some people call this situation the &amp;quot;Deadly Diamond of Death&amp;quot;. This is illustrated by the figure below:&lt;br /&gt;
&lt;br /&gt;
figure&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a composition but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Is-a&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming.&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
  module CarAction&lt;br /&gt;
    def goLeft(steps)&lt;br /&gt;
      Movement.new(self, steps)&lt;br /&gt;
    end&lt;br /&gt;
  end  &lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  #The Actions module can be included to allow a class to generate movements.&lt;br /&gt;
  class BigCar&lt;br /&gt;
    include CarAction&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside BigCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #After CarActions is included goLeft can be executed by any instance of BigCar.&lt;br /&gt;
  car1 = BigCar.new&lt;br /&gt;
  car1.carMethod&lt;br /&gt;
  movement1 = car1.goLeft&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  #The CarActions module is not included in the SmallCar class.&lt;br /&gt;
  class SmallCar&lt;br /&gt;
    def carMethod&lt;br /&gt;
      puts &amp;quot;This is a method inside SmallCar class&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  #Extend adds the methods to one instance, not to all instances.&lt;br /&gt;
  car2 = SmallCar.new&lt;br /&gt;
  car2.extend CarActions&lt;br /&gt;
  movement2 = car.goLeft&lt;br /&gt;
  &lt;br /&gt;
  car3 = SmallCar.new&lt;br /&gt;
  movement3 = car.goLeft      #Error: car3 does not extend CarActions, hence cannot call the goLeft method.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52311</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52311"/>
		<updated>2011-10-17T21:30:17Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; &amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
=&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
irb(main):002:0&amp;gt; r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
irb(main):003:0&amp;gt; r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):014:0&amp;gt; e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
irb(main):016:0&amp;gt; string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
irb(main):017:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):020:0&amp;gt; e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
irb(main):021:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword &amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Multiple Inheritance has several disadvantages that can lead to ambiguous code behavior either during compile time or run time. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different super-classes. &lt;br /&gt;
&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single sub-class. &lt;br /&gt;
&lt;br /&gt;
For example, Mixins help achieve this.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
&lt;br /&gt;
For example, in a class representing mortgages, one might factor out FIXED_RATE and ADJUSTABLE mortgages. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing them. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase re-usability and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
In Java, some people call this situation the &amp;quot;Deadly Diamond of Death&amp;quot;. This is illustrated by the figure below:&lt;br /&gt;
&lt;br /&gt;
figure&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a composition but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Is-a&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming.&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52310</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52310"/>
		<updated>2011-10-17T21:30:01Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Modules and Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; &amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
=&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
irb(main):002:0&amp;gt; r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
irb(main):003:0&amp;gt; r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):014:0&amp;gt; e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
irb(main):016:0&amp;gt; string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
irb(main):017:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):020:0&amp;gt; e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
irb(main):021:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. Modules definition is similar to classes just that we use the keyword&amp;lt;b&amp;gt;module&amp;lt;/b&amp;gt; instead of the &amp;lt;b&amp;gt;class&amp;lt;/b&amp;gt; keyword.&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Multiple Inheritance has several disadvantages that can lead to ambiguous code behavior either during compile time or run time. Ruby does not support direct Multiple Inheritance. But, Multiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
Given below is the Taggable-string example taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]. Suppose we want to add tags to Strings, we can define a ''Taggable'' module and include it into the class. &lt;br /&gt;
 &lt;br /&gt;
  require 'set'      # A collection of unordered values with no duplicates&lt;br /&gt;
  &lt;br /&gt;
  module Taggable&lt;br /&gt;
    attr_accessor :tags&lt;br /&gt;
    &lt;br /&gt;
    def taggable_setup&lt;br /&gt;
      @tags = Set.new&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def add_tag(tag)&lt;br /&gt;
      @tags &amp;lt;&amp;lt; tag&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def remove_tag(tag)&lt;br /&gt;
      @tags.delete(tag)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  class TaggableString &amp;lt; String&lt;br /&gt;
    include Taggable&lt;br /&gt;
    def initialize(*args)&lt;br /&gt;
      super&lt;br /&gt;
      taggable_setup&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  s = TaggableString.new('It is a bright, it is a sunny day')&lt;br /&gt;
  s.add_tag 'sentence'&lt;br /&gt;
  s.add_tag 'quotation'&lt;br /&gt;
  s.tags                          # =&amp;gt;    #&amp;lt;Set: {&amp;quot;sentence&amp;quot;, &amp;quot;quotation&amp;quot;}&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different super-classes. &lt;br /&gt;
&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single sub-class. &lt;br /&gt;
&lt;br /&gt;
For example, Mixins help achieve this.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
&lt;br /&gt;
For example, in a class representing mortgages, one might factor out FIXED_RATE and ADJUSTABLE mortgages. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing them. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase re-usability and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
In Java, some people call this situation the &amp;quot;Deadly Diamond of Death&amp;quot;. This is illustrated by the figure below:&lt;br /&gt;
&lt;br /&gt;
figure&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a composition but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Is-a&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming.&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a set-based object specification system, the objects are defined using a top-down approach by first specifying the abstract and using that abstract to create the specific.&amp;lt;br&amp;gt;&lt;br /&gt;
To describe a ''car'' object, we collect properties that are common to all the cars and create a ''car'' class that represents all cars and contains features that all cars should have.&amp;lt;br&amp;gt;&lt;br /&gt;
Then we use that car class to create new ''car'' objects or instances.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
In a prototype-based object specification system, the objects are defined using a bottom-up approach by first specifying the specific instance and modifying it as necessary to represent other instances.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the same ''car'' example. To describe a car Mercedes, we create an object first that contains its properties (Mercedes logo, black, sedan, fast). If we later discover a new car Lexus (Lexus logo, white), we specify what we know about Lexus and assume that the remainder in identical to the prototypical car Mercedes. This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52295</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52295"/>
		<updated>2011-10-17T20:27:18Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Regular Expressions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; &amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
=&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Ruby there is &amp;quot;Regexp&amp;quot; which is a Ruby object representing a Regular expression.Creating a Regexp object is similar to creating a string except for the usage of a forward slash to delimit it,rather than quote marks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
r = /my regular expression/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The regular expressions will match the string &amp;quot;my regular expression&amp;quot; anywhere in the string.Let's look at what we can put into regular expressions using Regexp.&lt;br /&gt;
Here's an example to retrieve the first match of /w.ll/ in the string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; string1=&amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;I will drill for a well in walla walla&amp;quot;&lt;br /&gt;
irb(main):002:0&amp;gt; r=Regexp.new(/w.ll/)&lt;br /&gt;
=&amp;gt; /w.ll/&lt;br /&gt;
irb(main):003:0&amp;gt; r.match(string1)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;will&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):014:0&amp;gt; e4=Regexp.new('\d')&lt;br /&gt;
=&amp;gt; /\d/&lt;br /&gt;
irb(main):016:0&amp;gt; string=&amp;quot;hello12 123&amp;quot;&lt;br /&gt;
=&amp;gt; &amp;quot;hello12 123&amp;quot;&lt;br /&gt;
irb(main):017:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example since \d matches only digits it selects the 1 from Hello12.In order to retrieve the digits&amp;quot;12&amp;quot; from &amp;quot;hello12&amp;quot; we need to use \w which recognizes all word character[0-9A-Z a-z_] .Here's an example showing it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):020:0&amp;gt; e4=Regexp.new('\d\w*')&lt;br /&gt;
=&amp;gt; /\d\w*/&lt;br /&gt;
irb(main):021:0&amp;gt; e4.match(string)&lt;br /&gt;
=&amp;gt; #&amp;lt;MatchData &amp;quot;12&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Multiple Inheritance has several disadvantages that can lead to ambiguous code behavior either during compile time or run time. Ruby does not support direct Multiple Inheritance. But, Mutiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
  Example - Taggable string&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different super-classes. &lt;br /&gt;
&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single sub-class. &lt;br /&gt;
&lt;br /&gt;
For example, Mixins help achieve this.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
&lt;br /&gt;
For example, in a class representing mortgages, one might factor out FIXED_RATE and ADJUSTABLE mortgages. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing them. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase re-usability and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
In Java, some people call this situation the &amp;quot;Deadly Diamond of Death&amp;quot;. This is illustrated by the figure below:&lt;br /&gt;
&lt;br /&gt;
figure&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a composition but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Is-a&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming.&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
    &lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  &lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52276</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52276"/>
		<updated>2011-10-17T17:54:49Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Regular Expressions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. Regular expression is sort of a string used to match to other strings.In ruby regular expressions are written in the format /pattern/modifiers where pattern is the regular expression and modifiers are the series of characters specifying the various options. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; &amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
=&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Multiple Inheritance has several disadvantages that can lead to ambiguous code behavior either during compile time or run time. Ruby does not support direct Multiple Inheritance. But, Mutiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
  Example - Taggable string&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different super-classes. &lt;br /&gt;
&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single sub-class. &lt;br /&gt;
&lt;br /&gt;
For example, Mixins help achieve this.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
&lt;br /&gt;
For example, in a class representing mortgages, one might factor out FIXED_RATE and ADJUSTABLE mortgages. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing them. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase re-usability and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
In Java, some people call this situation the &amp;quot;Deadly Diamond of Death&amp;quot;. This is illustrated by the figure below:&lt;br /&gt;
&lt;br /&gt;
figure&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a composition but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Is-a&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming.&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
The example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU] illustrates the concept of extending specific objects.&lt;br /&gt;
&lt;br /&gt;
Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&lt;br /&gt;
  class Person&lt;br /&gt;
    attr_reader :name, :age, :occupation&lt;br /&gt;
&lt;br /&gt;
    def initialize(name, age, occupation)&lt;br /&gt;
      @name, @age, @occupation = name, age, occupation&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      true&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Amy = Person.new('Amy Wilson', 23, 'student')&lt;br /&gt;
  John = Person.new('John Mason', 40, 'professor')&lt;br /&gt;
  Amy.mild_mannered?                                  # =&amp;gt; true &lt;br /&gt;
  John.mild_mannered?                                 # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
The above class describes all the people who are mild-mannered. What if there are some other people who are not as mild-mannered as they appear. That is they have some super-powers as well. &lt;br /&gt;
&lt;br /&gt;
  module SuperPowers&lt;br /&gt;
    def fly&lt;br /&gt;
      'Flying!'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def leap(what)&lt;br /&gt;
      &amp;quot;Leaping #{what} in a single bound!&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def mild_mannered?&lt;br /&gt;
      false&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    def superhero_name&lt;br /&gt;
      'Superman'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Here, we have two situations. Some objects of the person class are mild-mannered, while some other objects of the same person class have super powers with them. &lt;br /&gt;
&lt;br /&gt;
To achieve this, if we add super power functionality to the person class itself, then all the mild-mannered person objects will also ending having values for this property. If we use ''include'' to mix the SuperPowers module into the Person class, it will give every person super powers. Some people are bound to misuse such power.&lt;br /&gt;
&lt;br /&gt;
The only way to achieve this is to ''extend'' only those objects that have super powers instead of extending the entire class itself.&lt;br /&gt;
&lt;br /&gt;
  Amy.extend(SuperPowers)&lt;br /&gt;
  puts Amy.superhero_name          # =&amp;gt; Superman&lt;br /&gt;
  puts Amy.fly                     # =&amp;gt; Flying!&lt;br /&gt;
  puts Amy.leap(rocks)             # =&amp;gt; Leaping #{what} in a single bound!   &lt;br /&gt;
  puts Amy.mild_mannered?          # =&amp;gt; false             &lt;br /&gt;
  puts John.mild_mannered?         # =&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
Given below is another example that illustrates extending specific objects.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52272</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52272"/>
		<updated>2011-10-17T17:51:16Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Regular Expressions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. Regular expression is sort of a string used to match to other strings. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; &amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
=&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Multiple Inheritance has several disadvantages that can lead to ambiguous code behavior either during compile time or run time. Ruby does not support direct Multiple Inheritance. But, Mutiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
  Example - Taggable string&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different super-classes. &lt;br /&gt;
&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single sub-class. &lt;br /&gt;
&lt;br /&gt;
For example, Mixins help achieve this.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
&lt;br /&gt;
For example, in a class representing mortgages, one might factor out FIXED_RATE and ADJUSTABLE mortgages. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing them. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase re-usability and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
In Java, some people call this situation the &amp;quot;Deadly Diamond of Death&amp;quot;. This is illustrated by the figure below:&lt;br /&gt;
&lt;br /&gt;
figure&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a composition but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Is-a&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming.&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
Consider the example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU].&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52271</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c ap</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_ap&amp;diff=52271"/>
		<updated>2011-10-17T17:50:46Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Regular Expressions==&lt;br /&gt;
&lt;br /&gt;
Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. Regular expression is sort of a string used to match to other strings. To understand the power of regular expressions here is an example.&lt;br /&gt;
&lt;br /&gt;
In the following example the character 'c'  is replaced with &amp;quot;&amp;quot;.&lt;br /&gt;
Microsoft Windows [Version 6.1.7600]&lt;br /&gt;
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; &amp;quot;faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
=&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Modules and Mixins==&lt;br /&gt;
&lt;br /&gt;
==Mixins==&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
==Composing Modules==&lt;br /&gt;
&lt;br /&gt;
==Simulating Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Multiple Inheritance has several disadvantages that can lead to ambiguous code behavior either during compile time or run time. Ruby does not support direct Multiple Inheritance. But, Mutiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&lt;br /&gt;
&lt;br /&gt;
  Example - Taggable string&lt;br /&gt;
&lt;br /&gt;
==Advantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Although multiple inheritance has its disadvantages, there are a couple of good reasons for using multiple inheritance. Generally, multiple inheritance is used in one of the following ways:&lt;br /&gt;
&lt;br /&gt;
'''1. Multiple Independant Protocols'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class has to have features of independant classes. A class is created by inheriting or combining two or more completely different super-classes. &lt;br /&gt;
&lt;br /&gt;
For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&lt;br /&gt;
&lt;br /&gt;
This is used when a class need to created as a combination of different super classes. Several classes are created specially for subsequent combination. There is a mix and match of super-classes combined into a single sub-class. &lt;br /&gt;
&lt;br /&gt;
For example, Mixins help achieve this.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
&lt;br /&gt;
Modularity of the sub-parts of the classes is noticed and factored out into subclasses. This is used when the super-classes are modular and the modularity has to be factored out into subclasses.&lt;br /&gt;
&lt;br /&gt;
For example, in a class representing mortgages, one might factor out FIXED_RATE and ADJUSTABLE mortgages. &lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
&lt;br /&gt;
Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing them. In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
&lt;br /&gt;
For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
Another example&lt;br /&gt;
&lt;br /&gt;
==Disadvantages of Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Programmers use multiple inheritance to increase re-usability and consistency in the system. Although multiple inheritance is useful, it can lead to ambiguity and increased complexity if not used carefully. For this reason, some languages like Java, Ruby etc., do not support direct multiple inheritance. They provide different ways to achieve multiple inheritance like Interfaces, Mixins etc. The problems that arise due to multiple inheritance are as follows:&lt;br /&gt;
&lt;br /&gt;
'''1. Name collision'''&lt;br /&gt;
&lt;br /&gt;
Two features (instance variables or methods) with the same name are inherited from different super-classes. &lt;br /&gt;
&lt;br /&gt;
The super-classes may be correct and consistent. But the conflict arises when the sub-class inherit the two super-classes which have methods of the same name (for example, ''initialize()'').&lt;br /&gt;
In Java, some people call this situation the &amp;quot;Deadly Diamond of Death&amp;quot;. This is illustrated by the figure below:&lt;br /&gt;
&lt;br /&gt;
figure&lt;br /&gt;
&lt;br /&gt;
'''2. Repeated inheritance'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may result in a sub-class inheriting the same super-class more than once. Since there are multiple paths from the sub-class to its ancestor classes, a class can by mistake, end up inhering the same super-class more than once. This can go unnoticed and lead to ambiguity and increase the chances of errors. It is very difficult to trace the errors as well. &lt;br /&gt;
&lt;br /&gt;
'''3. Method combination'''&lt;br /&gt;
&lt;br /&gt;
This problem is similar to the name collision issue discussed above. An object may need to execute a method (for example, ''initialize()'') which has been defined in different super-classes. The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
&lt;br /&gt;
'''4. Implementation difficulties'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance can result it increased code complexity and implementation difficulties. In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. Finding methods needs a lot of search or redirection along the hierarchy.  &lt;br /&gt;
&lt;br /&gt;
'''5. Misuse'''&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance provides the ability to a sub-class to inherit from a parent class as many times as it wants. Also, a sub-class can inherit from as many parent classes as it wants. Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&lt;br /&gt;
&lt;br /&gt;
For example, consider a new ApplePie class which has to inherit features from Apple class and Cinnamon class. Programmers may consider this multiple inheritance because ApplePie contains Apple and Cinnamon. But, this is not the right way. Whenever a class wants to inherit another class, there should be a &amp;quot;is-a&amp;quot; relationship between the sub-class and super-class. Here, there is a &amp;quot;has-a&amp;quot; relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Apple&lt;br /&gt;
&lt;br /&gt;
ApplePie has-a Cinnamon&lt;br /&gt;
&lt;br /&gt;
The relationship can be a composition but not inheritance.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Is-a&amp;quot; relationship does not exist. The thumb rule to check if inheritance is allowed is to verify the &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
&lt;br /&gt;
===Resolution of Name conflicts or Collisions===&lt;br /&gt;
&lt;br /&gt;
Multiple inheritance may cause name conflicts when a sub-class inherits different super-classes that contain the methods or variables with the same name. This can be resolved in many ways.&lt;br /&gt;
&lt;br /&gt;
====Compound selectors in Ruby====&lt;br /&gt;
&lt;br /&gt;
Suppose a class ''Sub'' inherits two different methods for ''MethodOne'' from two different super-classes, ''SuperClassOne'' and ''SuperClassTwo''. &lt;br /&gt;
&lt;br /&gt;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&lt;br /&gt;
&lt;br /&gt;
  SuperClassOne.MethodOne      #Uses the MethodOne method inherited from SuperClassOne&lt;br /&gt;
  SuperClassTwo.MethodOne      #Uses the MethodOne method inherited from SuperClassTwo&lt;br /&gt;
&lt;br /&gt;
====Renaming in Eiffel====&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming.&lt;br /&gt;
It contains a ''rename'' clause to remove name conflicts. This is illustrated below:&lt;br /&gt;
&lt;br /&gt;
  Class Sub inherit&lt;br /&gt;
    SuperClassOne rename x as x1, y as y1;&lt;br /&gt;
    SuperClassTwo rename x as x2, y as y2;&lt;br /&gt;
    feature....&lt;br /&gt;
&lt;br /&gt;
Here, the ''inherit'' clause would be illegal without the ''rename'' clause. This ensures that name conflicts are resolved. This also allows the programmer to give meaningful and appropriate names to the inherited features.&lt;br /&gt;
&lt;br /&gt;
==Specifying Objects==&lt;br /&gt;
&lt;br /&gt;
The Object-Oriented programming paradigm has two ways for specifying objects.&lt;br /&gt;
&lt;br /&gt;
===Set based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, a class is described which abstracts the features or properties of the object we want to specify.&lt;br /&gt;
&lt;br /&gt;
• After describing the template or class, instances or objects for that class are created to perform the actual work.&lt;br /&gt;
&lt;br /&gt;
• Classes can be described by Meta-classes.&lt;br /&gt;
&lt;br /&gt;
• Every object instance for a class is unique and holds its internal values for all the features defined for that class.&lt;br /&gt;
&lt;br /&gt;
• We can perform inheritance. More specific sub-classes for the described classes can be created that contain the properties of the parent, modify features of the parent or add additional properties.&lt;br /&gt;
&lt;br /&gt;
• When the object that receives a message to perform some action, if it does not understand, it consults its ancestor classes asking each of them to handle the message along the inheritance hierarchy.&lt;br /&gt;
&lt;br /&gt;
===Protocol based language - object specification===&lt;br /&gt;
&lt;br /&gt;
• First, an object that is a concrete representation of the object we are trying to specify is created. This is the prototype.&lt;br /&gt;
&lt;br /&gt;
• Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
&lt;br /&gt;
• Each and every instance has those properties and internal values that are specific and unique to itself and a reference to its prototype, called an extension.&lt;br /&gt;
&lt;br /&gt;
• Essentially, there is no distinction between an instance and a prototype. Any instance can be copied or cloned to become the prototypical object for its duplicates.&lt;br /&gt;
&lt;br /&gt;
• When an object receives a message, if it does not understand the message, the message is delegated to its prototypes asking each one of them to grant it the ability to handle the message.&lt;br /&gt;
&lt;br /&gt;
==Extending specific objects==&lt;br /&gt;
&lt;br /&gt;
===Ruby as a set-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can act as a set-based language.&lt;br /&gt;
&lt;br /&gt;
We can first define classes that form the blueprint or a template.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then create instances for the classes. These objects contain internal values for the properties present in the class.&amp;lt;br&amp;gt;&lt;br /&gt;
We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &amp;lt;br&amp;gt;&lt;br /&gt;
The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '''include''' statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
===Ruby as a prototype-based language===&lt;br /&gt;
&lt;br /&gt;
Ruby can also act as a prototype-based language. This is where specific instances of the class, not all, will have some unique features and properties.&lt;br /&gt;
&lt;br /&gt;
There is a way in Ruby, to add instance specific functionality to just specific instances/objects of a class by using the '''object#extend''' method.&lt;br /&gt;
&lt;br /&gt;
This means, some objects of a class can have values for all the properties mentioned in the class, but a few others can have additional properties specific to them. This concept is called '''Extending Objects'''.&lt;br /&gt;
&lt;br /&gt;
Consider the example below, which is taken from the [http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU].&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec6.pdf Class notes of CSC517, NCSU]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50263</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b jp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50263"/>
		<updated>2011-09-22T05:12:01Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Access control is defined as the ability for a modules implementation to remain hidden behind its public interface. This is achieved in object oriented languages via encapsulation. Access control is closely related to the encapsulation/information hiding principle of object-oriented languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The term “object oriented programming” was first used by Xerox PARC in [http://en.wikipedia.org/wiki/Smalltalk_programming_language Smalltalk programming language] so as to refer to the process of using objects as the foundation for computation. Since the introduction of OOP, a large number of modern programming languages are now using the concept. Some of these are [http://en.wikipedia.org/wiki/Fortran FORTRAN],[http://en.wikipedia.org/wiki/BASIC BASIC], and [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal]. There have been some compatibility issues, because many programs were not designed with a OOPs approach in mind.&lt;br /&gt;
To solve this researcher’s suggested designing languages that used OOP concepts and still retained many of the functions that programmers needed. Examples of this type are Eiffel, Java, and Ruby.&lt;br /&gt;
&lt;br /&gt;
== Need for Access Control In OO Languages ==&lt;br /&gt;
&lt;br /&gt;
One of the key OOP’s concept considered while designing programming languages is encapsulation. Encapsulation will allow a class to hide information from objects that may use the code. For example, the Cat class will have a purr() method. A code will be written which will explain how the cat purrs. Despite this, it is not necessary for Betsy the cat to know how she purrs. Encapsulation makes it easier for the developer to change the code without having to change the code for Betsy. When the structure of class is hidden, it is harder for it to be used in the wrong way. &lt;br /&gt;
&lt;br /&gt;
Encapsulation will basically state which class is allowed to use the members of a certain object. This concept makes it easier for programmers to deal with or avoid errors when developing a program. The members within OOP can be categorized as being '''protected, public, or private.'''&lt;br /&gt;
&lt;br /&gt;
== Access Control in various OO Languagues ==&lt;br /&gt;
&lt;br /&gt;
 	&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! &lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
|-&lt;br /&gt;
| Access &lt;br /&gt;
control&lt;br /&gt;
&lt;br /&gt;
|Public,protected,private,”friends&lt;br /&gt;
&lt;br /&gt;
|public,private,package,protected&lt;br /&gt;
&lt;br /&gt;
|private,public,protected&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
In [http://en.wikipedia.org/wiki/C%2B%2B C++] we have private, public and protected.&lt;br /&gt;
&lt;br /&gt;
==== Friend Class in C++ ====&lt;br /&gt;
Suppose variable “data” is a member of class stack and is private then it can accessed by members and friends of stack class. If the member is public then it has no access restrictions. But if the member is protected then it can be accessed by members and friends of stack class and those classes derived from stack class. The members in class are private by default and those of struct/union are public by default. The example below explains the access specifiers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
struct testA {&lt;br /&gt;
  	friend class testC;&lt;br /&gt;
        private:&lt;br /&gt;
               int a;&lt;br /&gt;
        public:&lt;br /&gt;
               int b;&lt;br /&gt;
        protected:&lt;br /&gt;
               int c;&lt;br /&gt;
};&lt;br /&gt;
struct testB : testA {&lt;br /&gt;
  	       void f() {&lt;br /&gt;
    	       a = 1;  // Cannot be accessed since a is private in testA&lt;br /&gt;
               b = 2;&lt;br /&gt;
    	       c = 3;&lt;br /&gt;
  	       }&lt;br /&gt;
};&lt;br /&gt;
struct testC {&lt;br /&gt;
  	void f(testA x) {&lt;br /&gt;
    		x.a = 4;&lt;br /&gt;
                x.b = 5;&lt;br /&gt;
    	        x.c = 6;&lt;br /&gt;
  		}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int main() {&lt;br /&gt;
  	testA y;&lt;br /&gt;
        y.a = 7;  // Cannot be accessed since a is private in testA&lt;br /&gt;
        y.b = 8;&lt;br /&gt;
        y.c = 9; // Cannot be accessed since c is protected in testA&lt;br /&gt;
&lt;br /&gt;
        testB z;&lt;br /&gt;
        z.a = 10; // Cannot be accessed since a is private in testA&lt;br /&gt;
        z.b = 11;&lt;br /&gt;
        z.c = 12; // Cannot be accessed since c is protected in testA&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Derived Class in C++ ====&lt;br /&gt;
C++ also provides access specifiers for derived classes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Base_Class&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    int Public_Func();    // Declare a public member.&lt;br /&gt;
protected:&lt;br /&gt;
    int Protected_Func(); // Declare a protected member.&lt;br /&gt;
private:&lt;br /&gt;
    int Private_Func();   // Declare a private member.&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Declare two classes derived from BaseClass.&lt;br /&gt;
class Derived_Class1 : public BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Derived_Class2 : private BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Derived_Class1, the member function Public_Func is a public member and Protected_Func is a protected member because Base_Class is a public base class. Private_Func is private to Base_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
In Derived_Class2, the functions Public_Func and Protected_Func are considered private members because Base_Class is a private base class. Again, Private_Func is private toBase_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] provides 4 types of access specifiers Public, Private, Protected and default.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Situation&lt;br /&gt;
! Public&lt;br /&gt;
! Protected&lt;br /&gt;
! default&lt;br /&gt;
! private&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from same Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from different Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no,unless it is&lt;br /&gt;
a subclass&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Public Methods in Java ====&lt;br /&gt;
Public methods, variables and classes can be accessed from anywhere in the file system. The constraint that there should be just one public class per file is enforced.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square &lt;br /&gt;
{ 	 // public class&lt;br /&gt;
  public x, y;   // public instance variables&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Private Methods in Java ====&lt;br /&gt;
Private variables and methods has only class level access. Not even subclasses can access those. Such kind off variables can be accessed by getter and setter methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square&lt;br /&gt;
 {   // public class&lt;br /&gt;
  	private double x, y;   // private (encapsulated) instance variables&lt;br /&gt;
&lt;br /&gt;
  	public setCorner(int x, int y) &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		this.x = x;&lt;br /&gt;
    		this.y = y;&lt;br /&gt;
  	}&lt;br /&gt;
&lt;br /&gt;
  	public getCorner() &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		return Point(x, y);&lt;br /&gt;
  	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Protected Methods in Java ====&lt;br /&gt;
Protected methods and variables can be accessed by the same class, its sub classes and those within the same package. While in Default the class, methods and fields have only package level access. Below illustrates an example on Protected and Private access specifiers.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class calculate&lt;br /&gt;
{&lt;br /&gt;
	protected double x,y;&lt;br /&gt;
	private double z;&lt;br /&gt;
&lt;br /&gt;
	calculate(double x, double y)&lt;br /&gt;
	{&lt;br /&gt;
		this.x = x;&lt;br /&gt;
		this.y = y;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class multiplication extends calculate&lt;br /&gt;
{&lt;br /&gt;
	void prod()&lt;br /&gt;
	{&lt;br /&gt;
		double result = x * y;&lt;br /&gt;
		// z = result;  // Since z is private&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby ===&lt;br /&gt;
The concept of private,protected and public methods in [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] is somewhere different than it is in languages like Java(the public concept is similar).&lt;br /&gt;
&lt;br /&gt;
==== Private Methods in Ruby ====&lt;br /&gt;
In Ruby when a method is declared as private,it means it can never be called with an explicit receiver. This typically means that we can call a private method from within a class it is declared as well as subclasses of this class.Below illustrates an example for private access specifier.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  private&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
A.new.main_method&lt;br /&gt;
B.new.main_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, as soon as we try to use an explicit receiver, even if the receiver is &amp;quot;self&amp;quot;, the method call will fail .Below illustrates an example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
C.new.main_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
a.rb:36:in `main_method': private method `method1' called for #&amp;lt;C:0x7f67025a0648&amp;gt; (NoMethodError)&lt;br /&gt;
    from a.rb:40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see that a private method can never be called from outside the class hierarchy where it is defined.&lt;br /&gt;
&lt;br /&gt;
==== Protected Methods in Ruby ====&lt;br /&gt;
&lt;br /&gt;
Protected methods can be called with an implicit receiver,just like a private method,but in addition to this it can be called with an explicit receiver as long as this receiver is self.Below illustrates an example of this:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  protected&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
hello from C&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see here unlike the private method ,call with implicit receiver in class B and explicit receiver in class C both succeed.&lt;br /&gt;
&lt;br /&gt;
==== Public Methods in Ruby ====&lt;br /&gt;
Public methods are accessible with any kind of explicit or implicit receiver from anywhere.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In OO languages one of the key principle is encapsulation/information hiding. The examples of different OO programming languages have been illustrated above and how access control helps in achieving encapsulation. With the progress of each OO language the access specifiers definition have been formulated to suit the behaviour of the language.&lt;br /&gt;
&lt;br /&gt;
In java 1.0 release there was the private protected specifier. It had the same behavior of the protected modifier in C++, keeping access to subclasses, but not package members. It didn't fall into the nice progression of adding access, going from private to default to protected to public, as it was more like going from private to private protected to public - bypassing default all together and was hence dropped.&lt;br /&gt;
&lt;br /&gt;
Thus access specifiers are an important concept in OO languages and their importance and need should be understood by programmer's for better programming.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1]http://msdn.microsoft.com/en-us/library/7f45fat0(v=vs.80).aspx&lt;br /&gt;
&lt;br /&gt;
[2]http://staff.science.uva.nl/~heck/JAVAcourse/ch4/ss2_2.html&lt;br /&gt;
&lt;br /&gt;
[3]http://en.wikipedia.org/wiki/Class_(computer_programming)&lt;br /&gt;
&lt;br /&gt;
[4]http://www.skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/&lt;br /&gt;
&lt;br /&gt;
[5]http://www.jvoegele.com/software/langcomp.html&lt;br /&gt;
&lt;br /&gt;
[6]http://www.jot.fm/issues/issue_2005_05/article3/&lt;br /&gt;
&lt;br /&gt;
[7]http://www.jguru.com/faq/view.jsp?EID=15576&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50262</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b jp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50262"/>
		<updated>2011-09-22T05:11:22Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Access control is defined as the ability for a modules implementation to remain hidden behind its public interface. This is achieved in object oriented languages via encapsulation. Access control is closely related to the encapsulation/information hiding principle of object-oriented languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The term “object oriented programming” was first used by Xerox PARC in [http://en.wikipedia.org/wiki/Smalltalk_programming_language Smalltalk programming language] so as to refer to the process of using objects as the foundation for computation. Since the introduction of OOP, a large number of modern programming languages are now using the concept. Some of these are [http://en.wikipedia.org/wiki/Fortran FORTRAN],[http://en.wikipedia.org/wiki/BASIC BASIC], and [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal]. There have been some compatibility issues, because many programs were not designed with a OOPs approach in mind.&lt;br /&gt;
To solve this researcher’s suggested designing languages that used OOP concepts and still retained many of the functions that programmers needed. Examples of this type are Eiffel, Java, and Ruby.&lt;br /&gt;
&lt;br /&gt;
== Need for Access Control In OO Languages ==&lt;br /&gt;
&lt;br /&gt;
One of the key OOP’s concept considered while designing programming languages is encapsulation. Encapsulation will allow a class to hide information from objects that may use the code. For example, the Cat class will have a purr() method. A code will be written which will explain how the cat purrs. Despite this, it is not necessary for Betsy the cat to know how she purrs. Encapsulation makes it easier for the developer to change the code without having to change the code for Betsy. When the structure of class is hidden, it is harder for it to be used in the wrong way. &lt;br /&gt;
&lt;br /&gt;
Encapsulation will basically state which class is allowed to use the members of a certain object. This concept makes it easier for programmers to deal with or avoid errors when developing a program. The members within OOP can be categorized as being '''protected, public, or private.'''&lt;br /&gt;
&lt;br /&gt;
== Access Control in various OO Languagues ==&lt;br /&gt;
&lt;br /&gt;
 	&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! &lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
|-&lt;br /&gt;
| Access &lt;br /&gt;
control&lt;br /&gt;
&lt;br /&gt;
|Public,protected,private,”friends&lt;br /&gt;
&lt;br /&gt;
|public,private,package,protected&lt;br /&gt;
&lt;br /&gt;
|private,public,protected&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
In [http://en.wikipedia.org/wiki/C%2B%2B C++] we have private, public and protected.&lt;br /&gt;
&lt;br /&gt;
==== Friend Class in C++ ====&lt;br /&gt;
Suppose variable “data” is a member of class stack and is private then it can accessed by members and friends of stack class. If the member is public then it has no access restrictions. But if the member is protected then it can be accessed by members and friends of stack class and those classes derived from stack class. The members in class are private by default and those of struct/union are public by default. The example below explains the access specifiers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
struct testA {&lt;br /&gt;
  	friend class testC;&lt;br /&gt;
        private:&lt;br /&gt;
               int a;&lt;br /&gt;
        public:&lt;br /&gt;
               int b;&lt;br /&gt;
        protected:&lt;br /&gt;
               int c;&lt;br /&gt;
};&lt;br /&gt;
struct testB : testA {&lt;br /&gt;
  	       void f() {&lt;br /&gt;
    	       a = 1;  // Cannot be accessed since a is private in testA&lt;br /&gt;
               b = 2;&lt;br /&gt;
    	       c = 3;&lt;br /&gt;
  	       }&lt;br /&gt;
};&lt;br /&gt;
struct testC {&lt;br /&gt;
  	void f(testA x) {&lt;br /&gt;
    		x.a = 4;&lt;br /&gt;
                x.b = 5;&lt;br /&gt;
    	        x.c = 6;&lt;br /&gt;
  		}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int main() {&lt;br /&gt;
  	testA y;&lt;br /&gt;
        y.a = 7;  // Cannot be accessed since a is private in testA&lt;br /&gt;
        y.b = 8;&lt;br /&gt;
        y.c = 9; // Cannot be accessed since c is protected in testA&lt;br /&gt;
&lt;br /&gt;
        testB z;&lt;br /&gt;
        z.a = 10; // Cannot be accessed since a is private in testA&lt;br /&gt;
        z.b = 11;&lt;br /&gt;
        z.c = 12; // Cannot be accessed since c is protected in testA&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Derived Class in C++ ====&lt;br /&gt;
C++ also provides access specifiers for derived classes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Base_Class&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    int Public_Func();    // Declare a public member.&lt;br /&gt;
protected:&lt;br /&gt;
    int Protected_Func(); // Declare a protected member.&lt;br /&gt;
private:&lt;br /&gt;
    int Private_Func();   // Declare a private member.&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Declare two classes derived from BaseClass.&lt;br /&gt;
class Derived_Class1 : public BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Derived_Class2 : private BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Derived_Class1, the member function Public_Func is a public member and Protected_Func is a protected member because Base_Class is a public base class. Private_Func is private to Base_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
In Derived_Class2, the functions Public_Func and Protected_Func are considered private members because Base_Class is a private base class. Again, Private_Func is private toBase_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] provides 4 types of access specifiers Public, Private, Protected and default.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Situation&lt;br /&gt;
! Public&lt;br /&gt;
! Protected&lt;br /&gt;
! default&lt;br /&gt;
! private&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from same Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from different Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no,unless it is&lt;br /&gt;
a subclass&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Public Methods in Java ====&lt;br /&gt;
Public methods, variables and classes can be accessed from anywhere in the file system. The constraint that there should be just one public class per file is enforced.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square &lt;br /&gt;
{ 	 // public class&lt;br /&gt;
  public x, y;   // public instance variables&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Private Methods in Java ====&lt;br /&gt;
Private variables and methods has only class level access. Not even subclasses can access those. Such kind off variables can be accessed by getter and setter methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square&lt;br /&gt;
 {   // public class&lt;br /&gt;
  	private double x, y;   // private (encapsulated) instance variables&lt;br /&gt;
&lt;br /&gt;
  	public setCorner(int x, int y) &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		this.x = x;&lt;br /&gt;
    		this.y = y;&lt;br /&gt;
  	}&lt;br /&gt;
&lt;br /&gt;
  	public getCorner() &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		return Point(x, y);&lt;br /&gt;
  	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Protected Methods in Java ====&lt;br /&gt;
Protected methods and variables can be accessed by the same class, its sub classes and those within the same package. While in Default the class, methods and fields have only package level access. Below illustrates an example on Protected and Private access specifiers.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class calculate&lt;br /&gt;
{&lt;br /&gt;
	protected double x,y;&lt;br /&gt;
	private double z;&lt;br /&gt;
&lt;br /&gt;
	calculate(double x, double y)&lt;br /&gt;
	{&lt;br /&gt;
		this.x = x;&lt;br /&gt;
		this.y = y;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class multiplication extends calculate&lt;br /&gt;
{&lt;br /&gt;
	void prod()&lt;br /&gt;
	{&lt;br /&gt;
		double result = x * y;&lt;br /&gt;
		// z = result;  // Since z is private&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby ===&lt;br /&gt;
The concept of private,protected and public methods in [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] is somewhere different than it is in languages like Java(the public concept is similar).&lt;br /&gt;
&lt;br /&gt;
==== Private Methods in Ruby ====&lt;br /&gt;
In Ruby when a method is declared as private,it means it can never be called with an explicit receiver. This typically means that we can call a private method from within a class it is declared as well as subclasses of this class.Below illustrates an example for private access specifier.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  private&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
A.new.main_method&lt;br /&gt;
B.new.main_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, as soon as we try to use an explicit receiver, even if the receiver is &amp;quot;self&amp;quot;, the method call will fail .Below illustrates an example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
C.new.main_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
a.rb:36:in `main_method': private method `method1' called for #&amp;lt;C:0x7f67025a0648&amp;gt; (NoMethodError)&lt;br /&gt;
    from a.rb:40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see that a private method can never be called from outside the class hierarchy where it is defined.&lt;br /&gt;
&lt;br /&gt;
 ==== Protected Methods in Ruby ====&lt;br /&gt;
&lt;br /&gt;
Protected methods can be called with an implicit receiver,just like a private method,but in addition to this it can be called with an explicit receiver as long as this receiver is self.Below illustrates an example of this:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  protected&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
hello from C&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see here unlike the private method ,call with implicit receiver in class B and explicit receiver in class C both succeed.&lt;br /&gt;
&lt;br /&gt;
==== Public Methods in Ruby ====&lt;br /&gt;
Public methods are accessible with any kind of explicit or implicit receiver from anywhere.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In OO languages one of the key principle is encapsulation/information hiding. The examples of different OO programming languages have been illustrated above and how access control helps in achieving encapsulation. With the progress of each OO language the access specifiers definition have been formulated to suit the behaviour of the language.&lt;br /&gt;
&lt;br /&gt;
In java 1.0 release there was the private protected specifier. It had the same behavior of the protected modifier in C++, keeping access to subclasses, but not package members. It didn't fall into the nice progression of adding access, going from private to default to protected to public, as it was more like going from private to private protected to public - bypassing default all together and was hence dropped.&lt;br /&gt;
&lt;br /&gt;
Thus access specifiers are an important concept in OO languages and their importance and need should be understood by programmer's for better programming.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1]http://msdn.microsoft.com/en-us/library/7f45fat0(v=vs.80).aspx&lt;br /&gt;
&lt;br /&gt;
[2]http://staff.science.uva.nl/~heck/JAVAcourse/ch4/ss2_2.html&lt;br /&gt;
&lt;br /&gt;
[3]http://en.wikipedia.org/wiki/Class_(computer_programming)&lt;br /&gt;
&lt;br /&gt;
[4]http://www.skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/&lt;br /&gt;
&lt;br /&gt;
[5]http://www.jvoegele.com/software/langcomp.html&lt;br /&gt;
&lt;br /&gt;
[6]http://www.jot.fm/issues/issue_2005_05/article3/&lt;br /&gt;
&lt;br /&gt;
[7]http://www.jguru.com/faq/view.jsp?EID=15576&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50261</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b jp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50261"/>
		<updated>2011-09-22T05:01:56Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Access control is defined as the ability for a modules implementation to remain hidden behind its public interface. This is achieved in object oriented languages via encapsulation. Access control is closely related to the encapsulation/information hiding principle of object-oriented languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The term “object oriented programming” was first used by Xerox PARC in [http://en.wikipedia.org/wiki/Smalltalk_programming_language Smalltalk programming language] so as to refer to the process of using objects as the foundation for computation. Since the introduction of OOP, a large number of modern programming languages are now using the concept. Some of these are [http://en.wikipedia.org/wiki/Fortran FORTRAN],[http://en.wikipedia.org/wiki/BASIC BASIC], and [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal]. There have been some compatibility issues, because many programs were not designed with a OOPs approach in mind.&lt;br /&gt;
To solve this researcher’s suggested designing languages that used OOP concepts and still retained many of the functions that programmers needed. Examples of this type are Eiffel, Java, and Ruby.&lt;br /&gt;
&lt;br /&gt;
== Need for Access Control In OO Languages ==&lt;br /&gt;
&lt;br /&gt;
One of the key OOP’s concept considered while designing programming languages is encapsulation. Encapsulation will allow a class to hide information from objects that may use the code. For example, the Cat class will have a purr() method. A code will be written which will explain how the cat purrs. Despite this, it is not necessary for Betsy the cat to know how she purrs. Encapsulation makes it easier for the developer to change the code without having to change the code for Betsy. When the structure of class is hidden, it is harder for it to be used in the wrong way. &lt;br /&gt;
&lt;br /&gt;
Encapsulation will basically state which class is allowed to use the members of a certain object. This concept makes it easier for programmers to deal with or avoid errors when developing a program. The members within OOP can be categorized as being '''protected, public, or private.'''&lt;br /&gt;
&lt;br /&gt;
== Access Control in various OO Languagues ==&lt;br /&gt;
&lt;br /&gt;
 	&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! &lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
|-&lt;br /&gt;
| Access &lt;br /&gt;
control&lt;br /&gt;
&lt;br /&gt;
|Public,protected,private,”friends&lt;br /&gt;
&lt;br /&gt;
|public,private,package,protected&lt;br /&gt;
&lt;br /&gt;
|private,public,protected&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
In [http://en.wikipedia.org/wiki/C%2B%2B C++] we have private, public and protected.&lt;br /&gt;
&lt;br /&gt;
==== Friend Class in C++ ====&lt;br /&gt;
Suppose variable “data” is a member of class stack and is private then it can accessed by members and friends of stack class. If the member is public then it has no access restrictions. But if the member is protected then it can be accessed by members and friends of stack class and those classes derived from stack class. The members in class are private by default and those of struct/union are public by default. The example below explains the access specifiers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
struct testA {&lt;br /&gt;
  	friend class testC;&lt;br /&gt;
        private:&lt;br /&gt;
               int a;&lt;br /&gt;
        public:&lt;br /&gt;
               int b;&lt;br /&gt;
        protected:&lt;br /&gt;
               int c;&lt;br /&gt;
};&lt;br /&gt;
struct testB : testA {&lt;br /&gt;
  	       void f() {&lt;br /&gt;
    	       a = 1;  // Cannot be accessed since a is private in testA&lt;br /&gt;
               b = 2;&lt;br /&gt;
    	       c = 3;&lt;br /&gt;
  	       }&lt;br /&gt;
};&lt;br /&gt;
struct testC {&lt;br /&gt;
  	void f(testA x) {&lt;br /&gt;
    		x.a = 4;&lt;br /&gt;
                x.b = 5;&lt;br /&gt;
    	        x.c = 6;&lt;br /&gt;
  		}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int main() {&lt;br /&gt;
  	testA y;&lt;br /&gt;
        y.a = 7;  // Cannot be accessed since a is private in testA&lt;br /&gt;
        y.b = 8;&lt;br /&gt;
        y.c = 9; // Cannot be accessed since c is protected in testA&lt;br /&gt;
&lt;br /&gt;
        testB z;&lt;br /&gt;
        z.a = 10; // Cannot be accessed since a is private in testA&lt;br /&gt;
        z.b = 11;&lt;br /&gt;
        z.c = 12; // Cannot be accessed since c is protected in testA&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Derived Class in C++ ====&lt;br /&gt;
C++ also provides access specifiers for derived classes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Base_Class&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    int Public_Func();    // Declare a public member.&lt;br /&gt;
protected:&lt;br /&gt;
    int Protected_Func(); // Declare a protected member.&lt;br /&gt;
private:&lt;br /&gt;
    int Private_Func();   // Declare a private member.&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Declare two classes derived from BaseClass.&lt;br /&gt;
class Derived_Class1 : public BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Derived_Class2 : private BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Derived_Class1, the member function Public_Func is a public member and Protected_Func is a protected member because Base_Class is a public base class. Private_Func is private to Base_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
In Derived_Class2, the functions Public_Func and Protected_Func are considered private members because Base_Class is a private base class. Again, Private_Func is private toBase_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] provides 4 types of access specifiers Public, Private, Protected and default.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Situation&lt;br /&gt;
! Public&lt;br /&gt;
! Protected&lt;br /&gt;
! default&lt;br /&gt;
! private&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from same Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from different Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no,unless it is&lt;br /&gt;
a subclass&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Public Methods in Java ====&lt;br /&gt;
Public methods, variables and classes can be accessed from anywhere in the file system. The constraint that there should be just one public class per file is enforced.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square &lt;br /&gt;
{ 	 // public class&lt;br /&gt;
  public x, y;   // public instance variables&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Private Methods in Java ====&lt;br /&gt;
Private variables and methods has only class level access. Not even subclasses can access those. Such kind off variables can be accessed by getter and setter methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square&lt;br /&gt;
 {   // public class&lt;br /&gt;
  	private double x, y;   // private (encapsulated) instance variables&lt;br /&gt;
&lt;br /&gt;
  	public setCorner(int x, int y) &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		this.x = x;&lt;br /&gt;
    		this.y = y;&lt;br /&gt;
  	}&lt;br /&gt;
&lt;br /&gt;
  	public getCorner() &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		return Point(x, y);&lt;br /&gt;
  	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Protected Methods in Java ====&lt;br /&gt;
Protected methods and variables can be accessed by the same class, its sub classes and those within the same package. While in Default the class, methods and fields have only package level access. Below illustrates an example on Protected and Private access specifiers.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class calculate&lt;br /&gt;
{&lt;br /&gt;
	protected double x,y;&lt;br /&gt;
	private double z;&lt;br /&gt;
&lt;br /&gt;
	calculate(double x, double y)&lt;br /&gt;
	{&lt;br /&gt;
		this.x = x;&lt;br /&gt;
		this.y = y;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class multiplication extends calculate&lt;br /&gt;
{&lt;br /&gt;
	void prod()&lt;br /&gt;
	{&lt;br /&gt;
		double result = x * y;&lt;br /&gt;
		// z = result;  // Since z is private&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby ===&lt;br /&gt;
The concept of private,protected and public methods in [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] is somewhere different than it is in languages like Java(the public concept is similar).&lt;br /&gt;
&lt;br /&gt;
==== Private Methods in Ruby ====&lt;br /&gt;
In Ruby when a method is declared as private,it means it can never be called with an explicit receiver. This typically means that we can call a private method from within a class it is declared as well as subclasses of this class.Below illustrates an example for private access specifier.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  private&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
A.new.main_method&lt;br /&gt;
B.new.main_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, as soon as we try to use an explicit receiver, even if the receiver is &amp;quot;self&amp;quot;, the method call will fail .Below illustrates an example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
C.new.main_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
a.rb:36:in `main_method': private method `method1' called for #&amp;lt;C:0x7f67025a0648&amp;gt; (NoMethodError)&lt;br /&gt;
    from a.rb:40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see that a private method can never be called from outside the class hierarchy where it is defined.&lt;br /&gt;
 ==== Protected Methods in Ruby ====&lt;br /&gt;
Protected methods can be called with an implicit receiver,just like a private method,but in addition to this it can be called with an explicit receiver as long as this receiver is self.Below illustrates an example of this:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  protected&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
hello from C&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see here unlike the private method ,call with implicit receiver in class B and explicit receiver in class C both succeed.&lt;br /&gt;
&lt;br /&gt;
==== Public Methods in Ruby ====&lt;br /&gt;
Public methods are accessible with any kind of explicit or implicit receiver from anywhere.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In OO languages one of the key principle is encapsulation/information hiding. The examples of different OO programming languages have been illustrated above and how access control helps in achieving encapsulation. With the progress of each OO language the access specifiers definition have been formulated to suit the behaviour of the language.&lt;br /&gt;
&lt;br /&gt;
In java 1.0 release there was the private protected specifier. It had the same behavior of the protected modifier in C++, keeping access to subclasses, but not package members. It didn't fall into the nice progression of adding access, going from private to default to protected to public, as it was more like going from private to private protected to public - bypassing default all together and was hence dropped.&lt;br /&gt;
&lt;br /&gt;
Thus access specifiers are an important concept in OO languages and their importance and need should be understood by programmer's for better programming.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1]http://msdn.microsoft.com/en-us/library/7f45fat0(v=vs.80).aspx&lt;br /&gt;
&lt;br /&gt;
[2]http://staff.science.uva.nl/~heck/JAVAcourse/ch4/ss2_2.html&lt;br /&gt;
&lt;br /&gt;
[3]http://en.wikipedia.org/wiki/Class_(computer_programming)&lt;br /&gt;
&lt;br /&gt;
[4]http://www.skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/&lt;br /&gt;
&lt;br /&gt;
[5]http://www.jvoegele.com/software/langcomp.html&lt;br /&gt;
&lt;br /&gt;
[6]http://www.jot.fm/issues/issue_2005_05/article3/&lt;br /&gt;
&lt;br /&gt;
[7]http://www.jguru.com/faq/view.jsp?EID=15576&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50260</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b jp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50260"/>
		<updated>2011-09-22T05:00:18Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Access control is defined as the ability for a modules implementation to remain hidden behind its public interface. This is achieved in object oriented languages via encapsulation. Access control is closely related to the encapsulation/information hiding principle of object-oriented languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The term “object oriented programming” was first used by Xerox PARC in [http://en.wikipedia.org/wiki/Smalltalk_programming_language Smalltalk programming language] so as to refer to the process of using objects as the foundation for computation. Since the introduction of OOP, a large number of modern programming languages are now using the concept. Some of these are [http://en.wikipedia.org/wiki/Fortran FORTRAN],[http://en.wikipedia.org/wiki/BASIC BASIC], and [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal]. There have been some compatibility issues, because many programs were not designed with a OOPs approach in mind.&lt;br /&gt;
To solve this researcher’s suggested designing languages that used OOP concepts and still retained many of the functions that programmers needed. Examples of this type are Eiffel, Java, and Ruby.&lt;br /&gt;
&lt;br /&gt;
== Need for Access Control In OO Languages ==&lt;br /&gt;
&lt;br /&gt;
One of the key OOP’s concept considered while designing programming languages is encapsulation. Encapsulation will allow a class to hide information from objects that may use the code. For example, the Cat class will have a purr() method. A code will be written which will explain how the cat purrs. Despite this, it is not necessary for Betsy the cat to know how she purrs. Encapsulation makes it easier for the developer to change the code without having to change the code for Betsy. When the structure of class is hidden, it is harder for it to be used in the wrong way. &lt;br /&gt;
&lt;br /&gt;
Encapsulation will basically state which class is allowed to use the members of a certain object. This concept makes it easier for programmers to deal with or avoid errors when developing a program. The members within OOP can be categorized as being '''protected, public, or private.'''&lt;br /&gt;
&lt;br /&gt;
== Access Control in various OO Languagues ==&lt;br /&gt;
&lt;br /&gt;
 	&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! &lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
|-&lt;br /&gt;
| Access &lt;br /&gt;
control&lt;br /&gt;
&lt;br /&gt;
|Public,protected,private,”friends&lt;br /&gt;
&lt;br /&gt;
|public,private,package,protected&lt;br /&gt;
&lt;br /&gt;
|private,public,protected&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
In [http://en.wikipedia.org/wiki/C%2B%2B C++] we have private, public and protected.&lt;br /&gt;
&lt;br /&gt;
==== Friend Class in C++ ====&lt;br /&gt;
Suppose variable “data” is a member of class stack and is private then it can accessed by members and friends of stack class. If the member is public then it has no access restrictions. But if the member is protected then it can be accessed by members and friends of stack class and those classes derived from stack class. The members in class are private by default and those of struct/union are public by default. The example below explains the access specifiers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
struct testA {&lt;br /&gt;
  	friend class testC;&lt;br /&gt;
        private:&lt;br /&gt;
               int a;&lt;br /&gt;
        public:&lt;br /&gt;
               int b;&lt;br /&gt;
        protected:&lt;br /&gt;
               int c;&lt;br /&gt;
};&lt;br /&gt;
struct testB : testA {&lt;br /&gt;
  	       void f() {&lt;br /&gt;
    	       a = 1;  // Cannot be accessed since a is private in testA&lt;br /&gt;
               b = 2;&lt;br /&gt;
    	       c = 3;&lt;br /&gt;
  	       }&lt;br /&gt;
};&lt;br /&gt;
struct testC {&lt;br /&gt;
  	void f(testA x) {&lt;br /&gt;
    		x.a = 4;&lt;br /&gt;
                x.b = 5;&lt;br /&gt;
    	        x.c = 6;&lt;br /&gt;
  		}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int main() {&lt;br /&gt;
  	testA y;&lt;br /&gt;
        y.a = 7;  // Cannot be accessed since a is private in testA&lt;br /&gt;
        y.b = 8;&lt;br /&gt;
        y.c = 9; // Cannot be accessed since c is protected in testA&lt;br /&gt;
&lt;br /&gt;
        testB z;&lt;br /&gt;
        z.a = 10; // Cannot be accessed since a is private in testA&lt;br /&gt;
        z.b = 11;&lt;br /&gt;
        z.c = 12; // Cannot be accessed since c is protected in testA&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Derived Class in C++ ====&lt;br /&gt;
C++ also provides access specifiers for derived classes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Base_Class&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    int Public_Func();    // Declare a public member.&lt;br /&gt;
protected:&lt;br /&gt;
    int Protected_Func(); // Declare a protected member.&lt;br /&gt;
private:&lt;br /&gt;
    int Private_Func();   // Declare a private member.&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Declare two classes derived from BaseClass.&lt;br /&gt;
class Derived_Class1 : public BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Derived_Class2 : private BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Derived_Class1, the member function Public_Func is a public member and Protected_Func is a protected member because Base_Class is a public base class. Private_Func is private to Base_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
In Derived_Class2, the functions Public_Func and Protected_Func are considered private members because Base_Class is a private base class. Again, Private_Func is private toBase_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] provides 4 types of access specifiers Public, Private, Protected and default.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Situation&lt;br /&gt;
! Public&lt;br /&gt;
! Protected&lt;br /&gt;
! default&lt;br /&gt;
! private&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from same Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from different Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no,unless it is&lt;br /&gt;
a subclass&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Public Methods in Java ====&lt;br /&gt;
Public methods, variables and classes can be accessed from anywhere in the file system. The constraint that there should be just one public class per file is enforced.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square &lt;br /&gt;
{ 	 // public class&lt;br /&gt;
  public x, y;   // public instance variables&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Private Methods in Java ====&lt;br /&gt;
Private variables and methods has only class level access. Not even subclasses can access those. Such kind off variables can be accessed by getter and setter methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square&lt;br /&gt;
 {   // public class&lt;br /&gt;
  	private double x, y;   // private (encapsulated) instance variables&lt;br /&gt;
&lt;br /&gt;
  	public setCorner(int x, int y) &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		this.x = x;&lt;br /&gt;
    		this.y = y;&lt;br /&gt;
  	}&lt;br /&gt;
&lt;br /&gt;
  	public getCorner() &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		return Point(x, y);&lt;br /&gt;
  	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Protected Methods in Java ====&lt;br /&gt;
Protected methods and variables can be accessed by the same class, its sub classes and those within the same package. While in Default the class, methods and fields have only package level access. Below illustrates an example on Protected and Private access specifiers.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class calculate&lt;br /&gt;
{&lt;br /&gt;
	protected double x,y;&lt;br /&gt;
	private double z;&lt;br /&gt;
&lt;br /&gt;
	calculate(double x, double y)&lt;br /&gt;
	{&lt;br /&gt;
		this.x = x;&lt;br /&gt;
		this.y = y;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class multiplication extends calculate&lt;br /&gt;
{&lt;br /&gt;
	void prod()&lt;br /&gt;
	{&lt;br /&gt;
		double result = x * y;&lt;br /&gt;
		// z = result;  // Since z is private&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby ===&lt;br /&gt;
The concept of private,protected and public methods in [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] is somewhere different than it is in languages like Java(the public concept is similar).&lt;br /&gt;
&lt;br /&gt;
In Ruby when a method is declared as private,it means it can never be called with an explicit receiver. This typically means that we can call a private method from within a class it is declared as well as subclasses of this class.Below illustrates an example for private access specifier.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  private&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
A.new.main_method&lt;br /&gt;
B.new.main_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, as soon as we try to use an explicit receiver, even if the receiver is &amp;quot;self&amp;quot;, the method call will fail .Below illustrates an example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
C.new.main_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
a.rb:36:in `main_method': private method `method1' called for #&amp;lt;C:0x7f67025a0648&amp;gt; (NoMethodError)&lt;br /&gt;
    from a.rb:40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see that a private method can never be called from outside the class hierarchy where it is defined.&lt;br /&gt;
&lt;br /&gt;
Protected methods can be called with an implicit receiver,just like a private method,but in addition to this it can be called with an explicit receiver as long as this receiver is self.Below illustrates an example of this:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  protected&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
hello from C&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see here unlike the private method ,call with implicit receiver in class B and explicit receiver in class C both succeed.&lt;br /&gt;
&lt;br /&gt;
Public methods are accessible with any kind of explicit or implicit receiver from anywhere.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In OO languages one of the key principle is encapsulation/information hiding. The examples of different OO programming languages have been illustrated above and how access control helps in achieving encapsulation. With the progress of each OO language the access specifiers definition have been formulated to suit the behaviour of the language.&lt;br /&gt;
&lt;br /&gt;
In java 1.0 release there was the private protected specifier. It had the same behavior of the protected modifier in C++, keeping access to subclasses, but not package members. It didn't fall into the nice progression of adding access, going from private to default to protected to public, as it was more like going from private to private protected to public - bypassing default all together and was hence dropped.&lt;br /&gt;
&lt;br /&gt;
Thus access specifiers are an important concept in OO languages and their importance and need should be understood by programmer's for better programming.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1]http://msdn.microsoft.com/en-us/library/7f45fat0(v=vs.80).aspx&lt;br /&gt;
&lt;br /&gt;
[2]http://staff.science.uva.nl/~heck/JAVAcourse/ch4/ss2_2.html&lt;br /&gt;
&lt;br /&gt;
[3]http://en.wikipedia.org/wiki/Class_(computer_programming)&lt;br /&gt;
&lt;br /&gt;
[4]http://www.skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/&lt;br /&gt;
&lt;br /&gt;
[5]http://www.jvoegele.com/software/langcomp.html&lt;br /&gt;
&lt;br /&gt;
[6]http://www.jot.fm/issues/issue_2005_05/article3/&lt;br /&gt;
&lt;br /&gt;
[7]http://www.jguru.com/faq/view.jsp?EID=15576&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50259</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b jp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50259"/>
		<updated>2011-09-22T04:59:05Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Access control is defined as the ability for a modules implementation to remain hidden behind its public interface. This is achieved in object oriented languages via encapsulation. Access control is closely related to the encapsulation/information hiding principle of object-oriented languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The term “object oriented programming” was first used by Xerox PARC in [http://en.wikipedia.org/wiki/Smalltalk_programming_language Smalltalk programming language] so as to refer to the process of using objects as the foundation for computation. Since the introduction of OOP, a large number of modern programming languages are now using the concept. Some of these are [http://en.wikipedia.org/wiki/Fortran FORTRAN],[http://en.wikipedia.org/wiki/BASIC BASIC], and [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal]. There have been some compatibility issues, because many programs were not designed with a OOPs approach in mind.&lt;br /&gt;
To solve this researcher’s suggested designing languages that used OOP concepts and still retained many of the functions that programmers needed. Examples of this type are Eiffel, Java, and Ruby.&lt;br /&gt;
&lt;br /&gt;
== Need for Access Control In OO Languages ==&lt;br /&gt;
&lt;br /&gt;
One of the key OOP’s concept considered while designing programming languages is encapsulation. Encapsulation will allow a class to hide information from objects that may use the code. For example, the Cat class will have a purr() method. A code will be written which will explain how the cat purrs. Despite this, it is not necessary for Betsy the cat to know how she purrs. Encapsulation makes it easier for the developer to change the code without having to change the code for Betsy. When the structure of class is hidden, it is harder for it to be used in the wrong way. &lt;br /&gt;
&lt;br /&gt;
Encapsulation will basically state which class is allowed to use the members of a certain object. This concept makes it easier for programmers to deal with or avoid errors when developing a program. The members within OOP can be categorized as being '''protected, public, or private.'''&lt;br /&gt;
&lt;br /&gt;
== Access Control in various OO Languagues ==&lt;br /&gt;
&lt;br /&gt;
 	&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! &lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
|-&lt;br /&gt;
| Access &lt;br /&gt;
control&lt;br /&gt;
&lt;br /&gt;
|Public,protected,private,”friends&lt;br /&gt;
&lt;br /&gt;
|public,private,package,protected&lt;br /&gt;
&lt;br /&gt;
|private,public,protected&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
In [http://en.wikipedia.org/wiki/C%2B%2B C++] we have private, public and protected.&lt;br /&gt;
&lt;br /&gt;
==== Friend Class in C++ ====&lt;br /&gt;
Suppose variable “data” is a member of class stack and is private then it can accessed by members and friends of stack class. If the member is public then it has no access restrictions. But if the member is protected then it can be accessed by members and friends of stack class and those classes derived from stack class. The members in class are private by default and those of struct/union are public by default. The example below explains the access specifiers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
struct testA {&lt;br /&gt;
  	friend class testC;&lt;br /&gt;
        private:&lt;br /&gt;
               int a;&lt;br /&gt;
        public:&lt;br /&gt;
               int b;&lt;br /&gt;
        protected:&lt;br /&gt;
               int c;&lt;br /&gt;
};&lt;br /&gt;
struct testB : testA {&lt;br /&gt;
  	       void f() {&lt;br /&gt;
    	       a = 1;  // Cannot be accessed since a is private in testA&lt;br /&gt;
               b = 2;&lt;br /&gt;
    	       c = 3;&lt;br /&gt;
  	       }&lt;br /&gt;
};&lt;br /&gt;
struct testC {&lt;br /&gt;
  	void f(testA x) {&lt;br /&gt;
    		x.a = 4;&lt;br /&gt;
                x.b = 5;&lt;br /&gt;
    	        x.c = 6;&lt;br /&gt;
  		}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int main() {&lt;br /&gt;
  	testA y;&lt;br /&gt;
        y.a = 7;  // Cannot be accessed since a is private in testA&lt;br /&gt;
        y.b = 8;&lt;br /&gt;
        y.c = 9; // Cannot be accessed since c is protected in testA&lt;br /&gt;
&lt;br /&gt;
        testB z;&lt;br /&gt;
        z.a = 10; // Cannot be accessed since a is private in testA&lt;br /&gt;
        z.b = 11;&lt;br /&gt;
        z.c = 12; // Cannot be accessed since c is protected in testA&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Derived Class in C++ ====&lt;br /&gt;
C++ also provides access specifiers for derived classes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Base_Class&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    int Public_Func();    // Declare a public member.&lt;br /&gt;
protected:&lt;br /&gt;
    int Protected_Func(); // Declare a protected member.&lt;br /&gt;
private:&lt;br /&gt;
    int Private_Func();   // Declare a private member.&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Declare two classes derived from BaseClass.&lt;br /&gt;
class Derived_Class1 : public BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Derived_Class2 : private BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Derived_Class1, the member function Public_Func is a public member and Protected_Func is a protected member because Base_Class is a public base class. Private_Func is private to Base_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
In Derived_Class2, the functions Public_Func and Protected_Func are considered private members because Base_Class is a private base class. Again, Private_Func is private toBase_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] provides 4 types of access specifiers Public, Private, Protected and default.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Situation&lt;br /&gt;
! Public&lt;br /&gt;
! Protected&lt;br /&gt;
! default&lt;br /&gt;
! private&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from same Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from different Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no,unless it is&lt;br /&gt;
a subclass&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Public methods, variables and classes can be accessed from anywhere in the file system. The constraint that there should be just one public class per file is enforced.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square &lt;br /&gt;
{ 	 // public class&lt;br /&gt;
  public x, y;   // public instance variables&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Private variables and methods has only class level access. Not even subclasses can access those. Such kind off variables can be accessed by getter and setter methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square&lt;br /&gt;
 {   // public class&lt;br /&gt;
  	private double x, y;   // private (encapsulated) instance variables&lt;br /&gt;
&lt;br /&gt;
  	public setCorner(int x, int y) &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		this.x = x;&lt;br /&gt;
    		this.y = y;&lt;br /&gt;
  	}&lt;br /&gt;
&lt;br /&gt;
  	public getCorner() &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		return Point(x, y);&lt;br /&gt;
  	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Protected methods and variables can be accessed by the same class, its sub classes and those within the same package. While in Default the class, methods and fields have only package level access. Below illustrates an example on Protected and Private access specifiers.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class calculate&lt;br /&gt;
{&lt;br /&gt;
	protected double x,y;&lt;br /&gt;
	private double z;&lt;br /&gt;
&lt;br /&gt;
	calculate(double x, double y)&lt;br /&gt;
	{&lt;br /&gt;
		this.x = x;&lt;br /&gt;
		this.y = y;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class multiplication extends calculate&lt;br /&gt;
{&lt;br /&gt;
	void prod()&lt;br /&gt;
	{&lt;br /&gt;
		double result = x * y;&lt;br /&gt;
		// z = result;  // Since z is private&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby ===&lt;br /&gt;
The concept of private,protected and public methods in [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] is somewhere different than it is in languages like Java(the public concept is similar).&lt;br /&gt;
&lt;br /&gt;
In Ruby when a method is declared as private,it means it can never be called with an explicit receiver. This typically means that we can call a private method from within a class it is declared as well as subclasses of this class.Below illustrates an example for private access specifier.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  private&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
A.new.main_method&lt;br /&gt;
B.new.main_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, as soon as we try to use an explicit receiver, even if the receiver is &amp;quot;self&amp;quot;, the method call will fail .Below illustrates an example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
C.new.main_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
a.rb:36:in `main_method': private method `method1' called for #&amp;lt;C:0x7f67025a0648&amp;gt; (NoMethodError)&lt;br /&gt;
    from a.rb:40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see that a private method can never be called from outside the class hierarchy where it is defined.&lt;br /&gt;
&lt;br /&gt;
Protected methods can be called with an implicit receiver,just like a private method,but in addition to this it can be called with an explicit receiver as long as this receiver is self.Below illustrates an example of this:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  protected&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
hello from C&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see here unlike the private method ,call with implicit receiver in class B and explicit receiver in class C both succeed.&lt;br /&gt;
&lt;br /&gt;
Public methods are accessible with any kind of explicit or implicit receiver from anywhere.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In OO languages one of the key principle is encapsulation/information hiding. The examples of different OO programming languages have been illustrated above and how access control helps in achieving encapsulation. With the progress of each OO language the access specifiers definition have been formulated to suit the behaviour of the language.&lt;br /&gt;
&lt;br /&gt;
In java 1.0 release there was the private protected specifier. It had the same behavior of the protected modifier in C++, keeping access to subclasses, but not package members. It didn't fall into the nice progression of adding access, going from private to default to protected to public, as it was more like going from private to private protected to public - bypassing default all together and was hence dropped.&lt;br /&gt;
&lt;br /&gt;
Thus access specifiers are an important concept in OO languages and their importance and need should be understood by programmer's for better programming.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1]http://msdn.microsoft.com/en-us/library/7f45fat0(v=vs.80).aspx&lt;br /&gt;
&lt;br /&gt;
[2]http://staff.science.uva.nl/~heck/JAVAcourse/ch4/ss2_2.html&lt;br /&gt;
&lt;br /&gt;
[3]http://en.wikipedia.org/wiki/Class_(computer_programming)&lt;br /&gt;
&lt;br /&gt;
[4]http://www.skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/&lt;br /&gt;
&lt;br /&gt;
[5]http://www.jvoegele.com/software/langcomp.html&lt;br /&gt;
&lt;br /&gt;
[6]http://www.jot.fm/issues/issue_2005_05/article3/&lt;br /&gt;
&lt;br /&gt;
[7]http://www.jguru.com/faq/view.jsp?EID=15576&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50258</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b jp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50258"/>
		<updated>2011-09-22T04:58:19Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Access control is defined as the ability for a modules implementation to remain hidden behind its public interface. This is achieved in object oriented languages via encapsulation. Access control is closely related to the encapsulation/information hiding principle of object-oriented languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The term “object oriented programming” was first used by Xerox PARC in [http://en.wikipedia.org/wiki/Smalltalk_programming_language Smalltalk programming language] so as to refer to the process of using objects as the foundation for computation. Since the introduction of OOP, a large number of modern programming languages are now using the concept. Some of these are [http://en.wikipedia.org/wiki/Fortran FORTRAN],[http://en.wikipedia.org/wiki/BASIC BASIC], and [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal]. There have been some compatibility issues, because many programs were not designed with a OOPs approach in mind.&lt;br /&gt;
To solve this researcher’s suggested designing languages that used OOP concepts and still retained many of the functions that programmers needed. Examples of this type are Eiffel, Java, and Ruby.&lt;br /&gt;
&lt;br /&gt;
== Need for Access Control In OO Languages ==&lt;br /&gt;
&lt;br /&gt;
One of the key OOP’s concept considered while designing programming languages is encapsulation. Encapsulation will allow a class to hide information from objects that may use the code. For example, the Cat class will have a purr() method. A code will be written which will explain how the cat purrs. Despite this, it is not necessary for Betsy the cat to know how she purrs. Encapsulation makes it easier for the developer to change the code without having to change the code for Betsy. When the structure of class is hidden, it is harder for it to be used in the wrong way. &lt;br /&gt;
&lt;br /&gt;
Encapsulation will basically state which class is allowed to use the members of a certain object. This concept makes it easier for programmers to deal with or avoid errors when developing a program. The members within OOP can be categorized as being '''protected, public, or private.'''&lt;br /&gt;
&lt;br /&gt;
== Access Control in various OO Languagues ==&lt;br /&gt;
&lt;br /&gt;
 	&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! &lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
|-&lt;br /&gt;
| Access &lt;br /&gt;
control&lt;br /&gt;
&lt;br /&gt;
|Public,protected,private,”friends&lt;br /&gt;
&lt;br /&gt;
|public,private,package,protected&lt;br /&gt;
&lt;br /&gt;
|private,public,protected&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
In [http://en.wikipedia.org/wiki/C%2B%2B C++] we have private, public and protected.&lt;br /&gt;
&lt;br /&gt;
==== Friend Class in C++ ====&lt;br /&gt;
Suppose variable “data” is a member of class stack and is private then it can accessed by members and friends of stack class. If the member is public then it has no access restrictions. But if the member is protected then it can be accessed by members and friends of stack class and those classes derived from stack class. The members in class are private by default and those of struct/union are public by default. The example below explains the access specifiers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
struct testA {&lt;br /&gt;
  	friend class testC;&lt;br /&gt;
        private:&lt;br /&gt;
               int a;&lt;br /&gt;
        public:&lt;br /&gt;
               int b;&lt;br /&gt;
        protected:&lt;br /&gt;
               int c;&lt;br /&gt;
};&lt;br /&gt;
struct testB : testA {&lt;br /&gt;
  	       void f() {&lt;br /&gt;
    	       a = 1;  // Cannot be accessed since a is private in testA&lt;br /&gt;
               b = 2;&lt;br /&gt;
    	       c = 3;&lt;br /&gt;
  	       }&lt;br /&gt;
};&lt;br /&gt;
struct testC {&lt;br /&gt;
  	void f(testA x) {&lt;br /&gt;
    		x.a = 4;&lt;br /&gt;
                x.b = 5;&lt;br /&gt;
    	        x.c = 6;&lt;br /&gt;
  		}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int main() {&lt;br /&gt;
  	testA y;&lt;br /&gt;
        y.a = 7;  // Cannot be accessed since a is private in testA&lt;br /&gt;
        y.b = 8;&lt;br /&gt;
        y.c = 9; // Cannot be accessed since c is protected in testA&lt;br /&gt;
&lt;br /&gt;
        testB z;&lt;br /&gt;
        z.a = 10; // Cannot be accessed since a is private in testA&lt;br /&gt;
        z.b = 11;&lt;br /&gt;
        z.c = 12; // Cannot be accessed since c is protected in testA&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 ==== Derived Class in C++ ====&lt;br /&gt;
C++ also provides access specifiers for derived classes.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Base_Class&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    int Public_Func();    // Declare a public member.&lt;br /&gt;
protected:&lt;br /&gt;
    int Protected_Func(); // Declare a protected member.&lt;br /&gt;
private:&lt;br /&gt;
    int Private_Func();   // Declare a private member.&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Declare two classes derived from BaseClass.&lt;br /&gt;
class Derived_Class1 : public BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Derived_Class2 : private BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Derived_Class1, the member function Public_Func is a public member and Protected_Func is a protected member because Base_Class is a public base class. Private_Func is private to Base_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
In Derived_Class2, the functions Public_Func and Protected_Func are considered private members because Base_Class is a private base class. Again, Private_Func is private toBase_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] provides 4 types of access specifiers Public, Private, Protected and default.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Situation&lt;br /&gt;
! Public&lt;br /&gt;
! Protected&lt;br /&gt;
! default&lt;br /&gt;
! private&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from same Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from different Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no,unless it is&lt;br /&gt;
a subclass&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Public methods, variables and classes can be accessed from anywhere in the file system. The constraint that there should be just one public class per file is enforced.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square &lt;br /&gt;
{ 	 // public class&lt;br /&gt;
  public x, y;   // public instance variables&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Private variables and methods has only class level access. Not even subclasses can access those. Such kind off variables can be accessed by getter and setter methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square&lt;br /&gt;
 {   // public class&lt;br /&gt;
  	private double x, y;   // private (encapsulated) instance variables&lt;br /&gt;
&lt;br /&gt;
  	public setCorner(int x, int y) &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		this.x = x;&lt;br /&gt;
    		this.y = y;&lt;br /&gt;
  	}&lt;br /&gt;
&lt;br /&gt;
  	public getCorner() &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		return Point(x, y);&lt;br /&gt;
  	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Protected methods and variables can be accessed by the same class, its sub classes and those within the same package. While in Default the class, methods and fields have only package level access. Below illustrates an example on Protected and Private access specifiers.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class calculate&lt;br /&gt;
{&lt;br /&gt;
	protected double x,y;&lt;br /&gt;
	private double z;&lt;br /&gt;
&lt;br /&gt;
	calculate(double x, double y)&lt;br /&gt;
	{&lt;br /&gt;
		this.x = x;&lt;br /&gt;
		this.y = y;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class multiplication extends calculate&lt;br /&gt;
{&lt;br /&gt;
	void prod()&lt;br /&gt;
	{&lt;br /&gt;
		double result = x * y;&lt;br /&gt;
		// z = result;  // Since z is private&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby ===&lt;br /&gt;
The concept of private,protected and public methods in [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] is somewhere different than it is in languages like Java(the public concept is similar).&lt;br /&gt;
&lt;br /&gt;
In Ruby when a method is declared as private,it means it can never be called with an explicit receiver. This typically means that we can call a private method from within a class it is declared as well as subclasses of this class.Below illustrates an example for private access specifier.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  private&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
A.new.main_method&lt;br /&gt;
B.new.main_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, as soon as we try to use an explicit receiver, even if the receiver is &amp;quot;self&amp;quot;, the method call will fail .Below illustrates an example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
C.new.main_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
a.rb:36:in `main_method': private method `method1' called for #&amp;lt;C:0x7f67025a0648&amp;gt; (NoMethodError)&lt;br /&gt;
    from a.rb:40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see that a private method can never be called from outside the class hierarchy where it is defined.&lt;br /&gt;
&lt;br /&gt;
Protected methods can be called with an implicit receiver,just like a private method,but in addition to this it can be called with an explicit receiver as long as this receiver is self.Below illustrates an example of this:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  protected&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
hello from C&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see here unlike the private method ,call with implicit receiver in class B and explicit receiver in class C both succeed.&lt;br /&gt;
&lt;br /&gt;
Public methods are accessible with any kind of explicit or implicit receiver from anywhere.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In OO languages one of the key principle is encapsulation/information hiding. The examples of different OO programming languages have been illustrated above and how access control helps in achieving encapsulation. With the progress of each OO language the access specifiers definition have been formulated to suit the behaviour of the language.&lt;br /&gt;
&lt;br /&gt;
In java 1.0 release there was the private protected specifier. It had the same behavior of the protected modifier in C++, keeping access to subclasses, but not package members. It didn't fall into the nice progression of adding access, going from private to default to protected to public, as it was more like going from private to private protected to public - bypassing default all together and was hence dropped.&lt;br /&gt;
&lt;br /&gt;
Thus access specifiers are an important concept in OO languages and their importance and need should be understood by programmer's for better programming.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1]http://msdn.microsoft.com/en-us/library/7f45fat0(v=vs.80).aspx&lt;br /&gt;
&lt;br /&gt;
[2]http://staff.science.uva.nl/~heck/JAVAcourse/ch4/ss2_2.html&lt;br /&gt;
&lt;br /&gt;
[3]http://en.wikipedia.org/wiki/Class_(computer_programming)&lt;br /&gt;
&lt;br /&gt;
[4]http://www.skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/&lt;br /&gt;
&lt;br /&gt;
[5]http://www.jvoegele.com/software/langcomp.html&lt;br /&gt;
&lt;br /&gt;
[6]http://www.jot.fm/issues/issue_2005_05/article3/&lt;br /&gt;
&lt;br /&gt;
[7]http://www.jguru.com/faq/view.jsp?EID=15576&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50257</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b jp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50257"/>
		<updated>2011-09-22T04:57:28Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Access control is defined as the ability for a modules implementation to remain hidden behind its public interface. This is achieved in object oriented languages via encapsulation. Access control is closely related to the encapsulation/information hiding principle of object-oriented languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The term “object oriented programming” was first used by Xerox PARC in [http://en.wikipedia.org/wiki/Smalltalk_programming_language Smalltalk programming language] so as to refer to the process of using objects as the foundation for computation. Since the introduction of OOP, a large number of modern programming languages are now using the concept. Some of these are [http://en.wikipedia.org/wiki/Fortran FORTRAN],[http://en.wikipedia.org/wiki/BASIC BASIC], and [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal]. There have been some compatibility issues, because many programs were not designed with a OOPs approach in mind.&lt;br /&gt;
To solve this researcher’s suggested designing languages that used OOP concepts and still retained many of the functions that programmers needed. Examples of this type are Eiffel, Java, and Ruby.&lt;br /&gt;
&lt;br /&gt;
== Need for Access Control In OO Languages ==&lt;br /&gt;
&lt;br /&gt;
One of the key OOP’s concept considered while designing programming languages is encapsulation. Encapsulation will allow a class to hide information from objects that may use the code. For example, the Cat class will have a purr() method. A code will be written which will explain how the cat purrs. Despite this, it is not necessary for Betsy the cat to know how she purrs. Encapsulation makes it easier for the developer to change the code without having to change the code for Betsy. When the structure of class is hidden, it is harder for it to be used in the wrong way. &lt;br /&gt;
&lt;br /&gt;
Encapsulation will basically state which class is allowed to use the members of a certain object. This concept makes it easier for programmers to deal with or avoid errors when developing a program. The members within OOP can be categorized as being '''protected, public, or private.'''&lt;br /&gt;
&lt;br /&gt;
== Access Control in various OO Languagues ==&lt;br /&gt;
&lt;br /&gt;
 	&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! &lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
|-&lt;br /&gt;
| Access &lt;br /&gt;
control&lt;br /&gt;
&lt;br /&gt;
|Public,protected,private,”friends&lt;br /&gt;
&lt;br /&gt;
|public,private,package,protected&lt;br /&gt;
&lt;br /&gt;
|private,public,protected&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
In [http://en.wikipedia.org/wiki/C%2B%2B C++] we have private, public and protected.&lt;br /&gt;
&lt;br /&gt;
==== Friend Class in C++ ====&lt;br /&gt;
Suppose variable “data” is a member of class stack and is private then it can accessed by members and friends of stack class. If the member is public then it has no access restrictions. But if the member is protected then it can be accessed by members and friends of stack class and those classes derived from stack class. The members in class are private by default and those of struct/union are public by default. The example below explains the access specifiers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
struct testA {&lt;br /&gt;
  	friend class testC;&lt;br /&gt;
        private:&lt;br /&gt;
               int a;&lt;br /&gt;
        public:&lt;br /&gt;
               int b;&lt;br /&gt;
        protected:&lt;br /&gt;
               int c;&lt;br /&gt;
};&lt;br /&gt;
struct testB : testA {&lt;br /&gt;
  	       void f() {&lt;br /&gt;
    	       a = 1;  // Cannot be accessed since a is private in testA&lt;br /&gt;
               b = 2;&lt;br /&gt;
    	       c = 3;&lt;br /&gt;
  	       }&lt;br /&gt;
};&lt;br /&gt;
struct testC {&lt;br /&gt;
  	void f(testA x) {&lt;br /&gt;
    		x.a = 4;&lt;br /&gt;
                x.b = 5;&lt;br /&gt;
    	        x.c = 6;&lt;br /&gt;
  		}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int main() {&lt;br /&gt;
  	testA y;&lt;br /&gt;
        y.a = 7;  // Cannot be accessed since a is private in testA&lt;br /&gt;
        y.b = 8;&lt;br /&gt;
        y.c = 9; // Cannot be accessed since c is protected in testA&lt;br /&gt;
&lt;br /&gt;
        testB z;&lt;br /&gt;
        z.a = 10; // Cannot be accessed since a is private in testA&lt;br /&gt;
        z.b = 11;&lt;br /&gt;
        z.c = 12; // Cannot be accessed since c is protected in testA&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
C++ also provides access specifiers for derived classes.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Base_Class&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    int Public_Func();    // Declare a public member.&lt;br /&gt;
protected:&lt;br /&gt;
    int Protected_Func(); // Declare a protected member.&lt;br /&gt;
private:&lt;br /&gt;
    int Private_Func();   // Declare a private member.&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Declare two classes derived from BaseClass.&lt;br /&gt;
class Derived_Class1 : public BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Derived_Class2 : private BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Derived_Class1, the member function Public_Func is a public member and Protected_Func is a protected member because Base_Class is a public base class. Private_Func is private to Base_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
In Derived_Class2, the functions Public_Func and Protected_Func are considered private members because Base_Class is a private base class. Again, Private_Func is private toBase_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] provides 4 types of access specifiers Public, Private, Protected and default.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Situation&lt;br /&gt;
! Public&lt;br /&gt;
! Protected&lt;br /&gt;
! default&lt;br /&gt;
! private&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from same Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from different Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no,unless it is&lt;br /&gt;
a subclass&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Public methods, variables and classes can be accessed from anywhere in the file system. The constraint that there should be just one public class per file is enforced.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square &lt;br /&gt;
{ 	 // public class&lt;br /&gt;
  public x, y;   // public instance variables&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Private variables and methods has only class level access. Not even subclasses can access those. Such kind off variables can be accessed by getter and setter methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square&lt;br /&gt;
 {   // public class&lt;br /&gt;
  	private double x, y;   // private (encapsulated) instance variables&lt;br /&gt;
&lt;br /&gt;
  	public setCorner(int x, int y) &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		this.x = x;&lt;br /&gt;
    		this.y = y;&lt;br /&gt;
  	}&lt;br /&gt;
&lt;br /&gt;
  	public getCorner() &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		return Point(x, y);&lt;br /&gt;
  	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Protected methods and variables can be accessed by the same class, its sub classes and those within the same package. While in Default the class, methods and fields have only package level access. Below illustrates an example on Protected and Private access specifiers.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class calculate&lt;br /&gt;
{&lt;br /&gt;
	protected double x,y;&lt;br /&gt;
	private double z;&lt;br /&gt;
&lt;br /&gt;
	calculate(double x, double y)&lt;br /&gt;
	{&lt;br /&gt;
		this.x = x;&lt;br /&gt;
		this.y = y;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class multiplication extends calculate&lt;br /&gt;
{&lt;br /&gt;
	void prod()&lt;br /&gt;
	{&lt;br /&gt;
		double result = x * y;&lt;br /&gt;
		// z = result;  // Since z is private&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby ===&lt;br /&gt;
The concept of private,protected and public methods in [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] is somewhere different than it is in languages like Java(the public concept is similar).&lt;br /&gt;
&lt;br /&gt;
In Ruby when a method is declared as private,it means it can never be called with an explicit receiver. This typically means that we can call a private method from within a class it is declared as well as subclasses of this class.Below illustrates an example for private access specifier.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  private&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
A.new.main_method&lt;br /&gt;
B.new.main_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, as soon as we try to use an explicit receiver, even if the receiver is &amp;quot;self&amp;quot;, the method call will fail .Below illustrates an example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
C.new.main_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
a.rb:36:in `main_method': private method `method1' called for #&amp;lt;C:0x7f67025a0648&amp;gt; (NoMethodError)&lt;br /&gt;
    from a.rb:40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see that a private method can never be called from outside the class hierarchy where it is defined.&lt;br /&gt;
&lt;br /&gt;
Protected methods can be called with an implicit receiver,just like a private method,but in addition to this it can be called with an explicit receiver as long as this receiver is self.Below illustrates an example of this:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  protected&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
hello from C&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see here unlike the private method ,call with implicit receiver in class B and explicit receiver in class C both succeed.&lt;br /&gt;
&lt;br /&gt;
Public methods are accessible with any kind of explicit or implicit receiver from anywhere.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In OO languages one of the key principle is encapsulation/information hiding. The examples of different OO programming languages have been illustrated above and how access control helps in achieving encapsulation. With the progress of each OO language the access specifiers definition have been formulated to suit the behaviour of the language.&lt;br /&gt;
&lt;br /&gt;
In java 1.0 release there was the private protected specifier. It had the same behavior of the protected modifier in C++, keeping access to subclasses, but not package members. It didn't fall into the nice progression of adding access, going from private to default to protected to public, as it was more like going from private to private protected to public - bypassing default all together and was hence dropped.&lt;br /&gt;
&lt;br /&gt;
Thus access specifiers are an important concept in OO languages and their importance and need should be understood by programmer's for better programming.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1]http://msdn.microsoft.com/en-us/library/7f45fat0(v=vs.80).aspx&lt;br /&gt;
&lt;br /&gt;
[2]http://staff.science.uva.nl/~heck/JAVAcourse/ch4/ss2_2.html&lt;br /&gt;
&lt;br /&gt;
[3]http://en.wikipedia.org/wiki/Class_(computer_programming)&lt;br /&gt;
&lt;br /&gt;
[4]http://www.skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/&lt;br /&gt;
&lt;br /&gt;
[5]http://www.jvoegele.com/software/langcomp.html&lt;br /&gt;
&lt;br /&gt;
[6]http://www.jot.fm/issues/issue_2005_05/article3/&lt;br /&gt;
&lt;br /&gt;
[7]http://www.jguru.com/faq/view.jsp?EID=15576&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50255</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b jp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50255"/>
		<updated>2011-09-22T04:50:16Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Access control is defined as the ability for a modules implementation to remain hidden behind its public interface. This is achieved in object oriented languages via encapsulation. Access control is closely related to the encapsulation/information hiding principle of object-oriented languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The term “object oriented programming” was first used by Xerox PARC in [http://en.wikipedia.org/wiki/Smalltalk_programming_language Smalltalk programming language] so as to refer to the process of using objects as the foundation for computation. Since the introduction of OOP, a large number of modern programming languages are now using the concept. Some of these are [http://en.wikipedia.org/wiki/Fortran FORTRAN],[http://en.wikipedia.org/wiki/BASIC BASIC], and [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal]. There have been some compatibility issues, because many programs were not designed with a OOPs approach in mind.&lt;br /&gt;
To solve this researcher’s suggested designing languages that used OOP concepts and still retained many of the functions that programmers needed. Examples of this type are Eiffel, Java, and Ruby.&lt;br /&gt;
&lt;br /&gt;
== Need for Access Control In OO Languages ==&lt;br /&gt;
&lt;br /&gt;
One of the key OOP’s concept considered while designing programming languages is encapsulation. Encapsulation will allow a class to hide information from objects that may use the code. For example, the Cat class will have a purr() method. A code will be written which will explain how the cat purrs. Despite this, it is not necessary for Betsy the cat to know how she purrs. Encapsulation makes it easier for the developer to change the code without having to change the code for Betsy. When the structure of class is hidden, it is harder for it to be used in the wrong way. &lt;br /&gt;
&lt;br /&gt;
Encapsulation will basically state which class is allowed to use the members of a certain object. This concept makes it easier for programmers to deal with or avoid errors when developing a program. The members within OOP can be categorized as being '''protected, public, or private.'''&lt;br /&gt;
&lt;br /&gt;
== Access Control in various OO Languagues ==&lt;br /&gt;
&lt;br /&gt;
 	&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! &lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
|-&lt;br /&gt;
| Access &lt;br /&gt;
control&lt;br /&gt;
&lt;br /&gt;
|Public,protected,private,”friends&lt;br /&gt;
&lt;br /&gt;
|public,private,package,protected&lt;br /&gt;
&lt;br /&gt;
|private,public,protected&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
In [http://en.wikipedia.org/wiki/C%2B%2B C++] we have private, public and protected. Suppose variable “data” is a member of class stack and is private then it can accessed by members and friends of stack class. If the member is public then it has no access restrictions. But if the member is protected then it can be accessed by members and friends of stack class and those classes derived from stack class. The members in class are private by default and those of struct/union are public by default. The example below explains the access specifiers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
struct testA {&lt;br /&gt;
  	friend class testC;&lt;br /&gt;
        private:&lt;br /&gt;
               int a;&lt;br /&gt;
        public:&lt;br /&gt;
               int b;&lt;br /&gt;
        protected:&lt;br /&gt;
               int c;&lt;br /&gt;
};&lt;br /&gt;
struct testB : testA {&lt;br /&gt;
  	       void f() {&lt;br /&gt;
    	       a = 1;  // Cannot be accessed since a is private in testA&lt;br /&gt;
               b = 2;&lt;br /&gt;
    	       c = 3;&lt;br /&gt;
  	       }&lt;br /&gt;
};&lt;br /&gt;
struct testC {&lt;br /&gt;
  	void f(testA x) {&lt;br /&gt;
    		x.a = 4;&lt;br /&gt;
                x.b = 5;&lt;br /&gt;
    	        x.c = 6;&lt;br /&gt;
  		}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int main() {&lt;br /&gt;
  	testA y;&lt;br /&gt;
        y.a = 7;  // Cannot be accessed since a is private in testA&lt;br /&gt;
        y.b = 8;&lt;br /&gt;
        y.c = 9; // Cannot be accessed since c is protected in testA&lt;br /&gt;
&lt;br /&gt;
        testB z;&lt;br /&gt;
        z.a = 10; // Cannot be accessed since a is private in testA&lt;br /&gt;
        z.b = 11;&lt;br /&gt;
        z.c = 12; // Cannot be accessed since c is protected in testA&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
C++ also provides access specifiers for derived classes.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Base_Class&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    int Public_Func();    // Declare a public member.&lt;br /&gt;
protected:&lt;br /&gt;
    int Protected_Func(); // Declare a protected member.&lt;br /&gt;
private:&lt;br /&gt;
    int Private_Func();   // Declare a private member.&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Declare two classes derived from BaseClass.&lt;br /&gt;
class Derived_Class1 : public BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Derived_Class2 : private BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Derived_Class1, the member function Public_Func is a public member and Protected_Func is a protected member because Base_Class is a public base class. Private_Func is private to Base_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
In Derived_Class2, the functions Public_Func and Protected_Func are considered private members because Base_Class is a private base class. Again, Private_Func is private toBase_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] provides 4 types of access specifiers Public, Private, Protected and default.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Situation&lt;br /&gt;
! Public&lt;br /&gt;
! Protected&lt;br /&gt;
! default&lt;br /&gt;
! private&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from same Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from different Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no,unless it is&lt;br /&gt;
a subclass&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Public methods, variables and classes can be accessed from anywhere in the file system. The constraint that there should be just one public class per file is enforced.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square &lt;br /&gt;
{ 	 // public class&lt;br /&gt;
  public x, y;   // public instance variables&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Private variables and methods has only class level access. Not even subclasses can access those. Such kind off variables can be accessed by getter and setter methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square&lt;br /&gt;
 {   // public class&lt;br /&gt;
  	private double x, y;   // private (encapsulated) instance variables&lt;br /&gt;
&lt;br /&gt;
  	public setCorner(int x, int y) &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		this.x = x;&lt;br /&gt;
    		this.y = y;&lt;br /&gt;
  	}&lt;br /&gt;
&lt;br /&gt;
  	public getCorner() &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		return Point(x, y);&lt;br /&gt;
  	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Protected methods and variables can be accessed by the same class, its sub classes and those within the same package. While in Default the class, methods and fields have only package level access. Below illustrates an example on Protected and Private access specifiers.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class calculate&lt;br /&gt;
{&lt;br /&gt;
	protected double x,y;&lt;br /&gt;
	private double z;&lt;br /&gt;
&lt;br /&gt;
	calculate(double x, double y)&lt;br /&gt;
	{&lt;br /&gt;
		this.x = x;&lt;br /&gt;
		this.y = y;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class multiplication extends calculate&lt;br /&gt;
{&lt;br /&gt;
	void prod()&lt;br /&gt;
	{&lt;br /&gt;
		double result = x * y;&lt;br /&gt;
		// z = result;  // Since z is private&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby ===&lt;br /&gt;
The concept of private,protected and public methods in [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] is somewhere different than it is in languages like Java(the public concept is similar).&lt;br /&gt;
&lt;br /&gt;
In Ruby when a method is declared as private,it means it can never be called with an explicit receiver. This typically means that we can call a private method from within a class it is declared as well as subclasses of this class.Below illustrates an example for private access specifier.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  private&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
A.new.main_method&lt;br /&gt;
B.new.main_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, as soon as we try to use an explicit receiver, even if the receiver is &amp;quot;self&amp;quot;, the method call will fail .Below illustrates an example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
C.new.main_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
a.rb:36:in `main_method': private method `method1' called for #&amp;lt;C:0x7f67025a0648&amp;gt; (NoMethodError)&lt;br /&gt;
    from a.rb:40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see that a private method can never be called from outside the class hierarchy where it is defined.&lt;br /&gt;
&lt;br /&gt;
Protected methods can be called with an implicit receiver,just like a private method,but in addition to this it can be called with an explicit receiver as long as this receiver is self.Below illustrates an example of this:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  protected&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
hello from C&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see here unlike the private method ,call with implicit receiver in class B and explicit receiver in class C both succeed.&lt;br /&gt;
&lt;br /&gt;
Public methods are accessible with any kind of explicit or implicit receiver from anywhere.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In OO languages one of the key principle is encapsulation/information hiding. The examples of different OO programming languages have been illustrated above and how access control helps in achieving encapsulation. With the progress of each OO language the access specifiers definition have been formulated to suit the behaviour of the language.&lt;br /&gt;
&lt;br /&gt;
In java 1.0 release there was the private protected specifier. It had the same behavior of the protected modifier in C++, keeping access to subclasses, but not package members. It didn't fall into the nice progression of adding access, going from private to default to protected to public, as it was more like going from private to private protected to public - bypassing default all together and was hence dropped.&lt;br /&gt;
&lt;br /&gt;
Thus access specifiers are an important concept in OO languages and their importance and need should be understood by programmer's for better programming.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1]http://msdn.microsoft.com/en-us/library/7f45fat0(v=vs.80).aspx&lt;br /&gt;
&lt;br /&gt;
[2]http://staff.science.uva.nl/~heck/JAVAcourse/ch4/ss2_2.html&lt;br /&gt;
&lt;br /&gt;
[3]http://en.wikipedia.org/wiki/Class_(computer_programming)&lt;br /&gt;
&lt;br /&gt;
[4]http://www.skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/&lt;br /&gt;
&lt;br /&gt;
[5]http://www.jvoegele.com/software/langcomp.html&lt;br /&gt;
&lt;br /&gt;
[6]http://www.jot.fm/issues/issue_2005_05/article3/&lt;br /&gt;
&lt;br /&gt;
[7]http://www.jguru.com/faq/view.jsp?EID=15576&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50254</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b jp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50254"/>
		<updated>2011-09-22T04:49:18Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Access control is defined as the ability for a modules implementation to remain hidden behind its public interface. This is achieved in object oriented languages via encapsulation. Access control is closely related to the encapsulation/information hiding principle of object-oriented languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The term “object oriented programming” was first used by Xerox PARC in [http://en.wikipedia.org/wiki/Smalltalk_programming_language Smalltalk programming language] so as to refer to the process of using objects as the foundation for computation. Since the introduction of OOP, a large number of modern programming languages are now using the concept. Some of these are [http://en.wikipedia.org/wiki/Fortran FORTRAN],[http://en.wikipedia.org/wiki/BASIC BASIC], and [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal]. There have been some compatibility issues, because many programs were not designed with a OOPs approach in mind.&lt;br /&gt;
To solve this researcher’s suggested designing languages that used OOP concepts and still retained many of the functions that programmers needed. Examples of this type are Eiffel, Java, and Ruby.&lt;br /&gt;
&lt;br /&gt;
== Need for Access Control In OO Languages ==&lt;br /&gt;
&lt;br /&gt;
One of the key OOP’s concept considered while designing programming languages is encapsulation. Encapsulation will allow a class to hide information from objects that may use the code. For example, the Cat class will have a purr() method. A code will be written which will explain how the cat purrs. Despite this, it is not necessary for Betsy the cat to know how she purrs. Encapsulation makes it easier for the developer to change the code without having to change the code for Betsy. When the structure of class is hidden, it is harder for it to be used in the wrong way. &lt;br /&gt;
&lt;br /&gt;
Encapsulation will basically state which class is allowed to use the members of a certain object. This concept makes it easier for programmers to deal with or avoid errors when developing a program. The members within OOP can be categorized as being '''protected, public, or private.'''&lt;br /&gt;
&lt;br /&gt;
== Access Control in various OO Languagues ==&lt;br /&gt;
&lt;br /&gt;
 	&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! &lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
|-&lt;br /&gt;
| Access &lt;br /&gt;
control&lt;br /&gt;
&lt;br /&gt;
|Public,protected,private,”friends&lt;br /&gt;
&lt;br /&gt;
|public,private,package,protected&lt;br /&gt;
&lt;br /&gt;
|private,public,protected&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
In [http://en.wikipedia.org/wiki/C%2B%2B C++] we have private, public and protected. Suppose variable “data” is a member of class stack and is private then it can accessed by members and friends of stack class. If the member is public then it has no access restrictions. But if the member is protected then it can be accessed by members and friends of stack class and those classes derived from stack class. The members in class are private by default and those of struct/union are public by default. The example below explains the access specifiers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
struct testA {&lt;br /&gt;
  	friend class testC;&lt;br /&gt;
        private:&lt;br /&gt;
               int a;&lt;br /&gt;
        public:&lt;br /&gt;
               int b;&lt;br /&gt;
        protected:&lt;br /&gt;
               int c;&lt;br /&gt;
};&lt;br /&gt;
struct testB : testA {&lt;br /&gt;
  	       void f() {&lt;br /&gt;
    	       a = 1;  // Cannot be accessed since a is private in testA&lt;br /&gt;
               b = 2;&lt;br /&gt;
    	       c = 3;&lt;br /&gt;
  	       }&lt;br /&gt;
};&lt;br /&gt;
struct testC {&lt;br /&gt;
  	void f(testA x) {&lt;br /&gt;
    		x.a = 4;&lt;br /&gt;
                x.b = 5;&lt;br /&gt;
    	        x.c = 6;&lt;br /&gt;
  		}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int main() {&lt;br /&gt;
  	testA y;&lt;br /&gt;
        y.a = 7;  // Cannot be accessed since a is private in testA&lt;br /&gt;
        y.b = 8;&lt;br /&gt;
        y.c = 9; // Cannot be accessed since c is protected in testA&lt;br /&gt;
&lt;br /&gt;
        testB z;&lt;br /&gt;
        z.a = 10; // Cannot be accessed since a is private in testA&lt;br /&gt;
        z.b = 11;&lt;br /&gt;
        z.c = 12; // Cannot be accessed since c is protected in testA&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
C++ also provides access specifiers for derived classes.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Base_Class&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    int Public_Func();    // Declare a public member.&lt;br /&gt;
protected:&lt;br /&gt;
    int Protected_Func(); // Declare a protected member.&lt;br /&gt;
private:&lt;br /&gt;
    int Private_Func();   // Declare a private member.&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Declare two classes derived from BaseClass.&lt;br /&gt;
class Derived_Class1 : public BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Derived_Class2 : private BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Derived_Class1, the member function Public_Func is a public member and Protected_Func is a protected member because Base_Class is a public base class. Private_Func is private to Base_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
In Derived_Class2, the functions Public_Func and Protected_Func are considered private members because Base_Class is a private base class. Again, Private_Func is private toBase_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] provides 4 types of access specifiers Public, Private, Protected and default.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Situation&lt;br /&gt;
! Public&lt;br /&gt;
! Protected&lt;br /&gt;
! default&lt;br /&gt;
! private&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from same Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from different Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no,unless it is&lt;br /&gt;
a subclass&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Public methods, variables and classes can be accessed from anywhere in the file system. The constraint that there should be just one public class per file is enforced.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square &lt;br /&gt;
{ 	 // public class&lt;br /&gt;
  public x, y;   // public instance variables&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Private variables and methods has only class level access. Not even subclasses can access those. Such kind off variables can be accessed by getter and setter methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square&lt;br /&gt;
 {   // public class&lt;br /&gt;
  	private double x, y;   // private (encapsulated) instance variables&lt;br /&gt;
&lt;br /&gt;
  	public setCorner(int x, int y) &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		this.x = x;&lt;br /&gt;
    		this.y = y;&lt;br /&gt;
  	}&lt;br /&gt;
&lt;br /&gt;
  	public getCorner() &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		return Point(x, y);&lt;br /&gt;
  	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Protected methods and variables can be accessed by the same class, its sub classes and those within the same package. While in Default the class, methods and fields have only package level access. Below illustrates an example on Protected and Private access specifiers.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class calculate&lt;br /&gt;
{&lt;br /&gt;
	protected double x,y;&lt;br /&gt;
	private double z;&lt;br /&gt;
&lt;br /&gt;
	calculate(double x, double y)&lt;br /&gt;
	{&lt;br /&gt;
		this.x = x;&lt;br /&gt;
		this.y = y;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class multiplication extends calculate&lt;br /&gt;
{&lt;br /&gt;
	void prod()&lt;br /&gt;
	{&lt;br /&gt;
		double result = x * y;&lt;br /&gt;
		// z = result;  // Since z is private&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby ===&lt;br /&gt;
The concept of private,protected and public methods in [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] is somewhere different than it is in languages like Java(the public concept is similar).&lt;br /&gt;
&lt;br /&gt;
In Ruby when a method is declared as private,it means it can never be called with an explicit receiver. This typically means that we can call a private method from within a class it is declared as well as subclasses of this class.Below illustrates an example for private access specifier.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  private&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
A.new.main_method&lt;br /&gt;
B.new.main_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, as soon as we try to use an explicit receiver, even if the receiver is &amp;quot;self&amp;quot;, the method call will fail .Below illustrates an example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
C.new.main_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
a.rb:36:in `main_method': private method `method1' called for #&amp;lt;C:0x7f67025a0648&amp;gt; (NoMethodError)&lt;br /&gt;
    from a.rb:40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see that a private method can never be called from outside the class hierarchy where it is defined.&lt;br /&gt;
&lt;br /&gt;
Protected methods can be called with an implicit receiver,just like a private method,but in addition to this it can be called with an explicit receiver as long as this receiver is self.Below illustrates an example of this:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  protected&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
hello from C&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see here unlike the private method ,call with implicit receiver in class B and explicit receiver in class C both succeed.&lt;br /&gt;
&lt;br /&gt;
Public methods are accessible with any kind of explicit or implicit receiver from anywhere.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In OO languages one of the key principle is encapsulation/data hiding. The examples of different OO programming languages have been illustrated above and how access control helps in achieving encapsulation. With the progress of each OO language the access specifiers definition have been formulated to suit the behaviour of the language.&lt;br /&gt;
&lt;br /&gt;
In java 1.0 release there was the private protected specifier. It had the same behavior of the protected modifier in C++, keeping access to subclasses, but not package members. It didn't fall into the nice progression of adding access, going from private to default to protected to public, as it was more like going from private to private protected to public - bypassing default all together and was hence dropped.&lt;br /&gt;
&lt;br /&gt;
Thus access specifiers are an important concept in OO languages and their importance and need should be understood by programmer's for better progrmming.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1]http://msdn.microsoft.com/en-us/library/7f45fat0(v=vs.80).aspx&lt;br /&gt;
&lt;br /&gt;
[2]http://staff.science.uva.nl/~heck/JAVAcourse/ch4/ss2_2.html&lt;br /&gt;
&lt;br /&gt;
[3]http://en.wikipedia.org/wiki/Class_(computer_programming)&lt;br /&gt;
&lt;br /&gt;
[4]http://www.skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/&lt;br /&gt;
&lt;br /&gt;
[5]http://www.jvoegele.com/software/langcomp.html&lt;br /&gt;
&lt;br /&gt;
[6]http://www.jot.fm/issues/issue_2005_05/article3/&lt;br /&gt;
&lt;br /&gt;
[7]http://www.jguru.com/faq/view.jsp?EID=15576&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50253</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b jp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50253"/>
		<updated>2011-09-22T04:48:30Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Access control is defined as the ability for a modules implementation to remain hidden behind its public interface. This is achieved in object oriented languages via encapsulation. Access control is closely related to the encapsulation/information hiding principle of object-oriented languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The term “object oriented programming” was first used by Xerox PARC in [http://en.wikipedia.org/wiki/Smalltalk_programming_language Smalltalk programming language] so as to refer to the process of using objects as the foundation for computation. Since the introduction of OOP, a large number of modern programming languages are now using the concept. Some of these are [http://en.wikipedia.org/wiki/Fortran FORTRAN],[http://en.wikipedia.org/wiki/BASIC BASIC], and [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal]. There have been some compatibility issues, because many programs were not designed with a OOPs approach in mind.&lt;br /&gt;
To solve this researcher’s suggested designing languages that used OOP concepts and still retained many of the functions that programmers needed. Examples of this type are Eiffel, Java, and Ruby.&lt;br /&gt;
&lt;br /&gt;
== Need for Access Control In OO Languages ==&lt;br /&gt;
&lt;br /&gt;
One of the key OOP’s concept considered while designing programming languages is encapsulation. Encapsulation will allow a class to hide information from objects that may use the code. For example, the Cat class will have a purr() method. A code will be written which will explain how the cat purrs. Despite this, it is not necessary for Betsy the cat to know how she purrs. Encapsulation makes it easier for the developer to change the code without having to change the code for Betsy. When the structure of class is hidden, it is harder for it to be used in the wrong way. &lt;br /&gt;
&lt;br /&gt;
Encapsulation will basically state which class is allowed to use the members of a certain object. This concept makes it easier for programmers to deal with or avoid errors when developing a program. The members within OOP can be categorized as being '''protected, public, or private.'''&lt;br /&gt;
&lt;br /&gt;
== Access Control in various OO Languagues ==&lt;br /&gt;
&lt;br /&gt;
 	&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! &lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
|-&lt;br /&gt;
| Access &lt;br /&gt;
control&lt;br /&gt;
&lt;br /&gt;
|Public,protected,private,”friends&lt;br /&gt;
&lt;br /&gt;
|public,private,package,protected&lt;br /&gt;
&lt;br /&gt;
|private,public,protected&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
In [http://en.wikipedia.org/wiki/C%2B%2B C++] we have private, public and protected. Suppose variable “data” is a member of class stack and is private then it can accessed by members and friends of stack class. If the member is public then it has no access restrictions. But if the member is protected then it can be accessed by members and friends of stack class and those classes derived from stack class. The members in class are private by default and those of struct/union are public by default. The example below explains the access specifiers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
struct testA {&lt;br /&gt;
  	friend class testC;&lt;br /&gt;
        private:&lt;br /&gt;
               int a;&lt;br /&gt;
        public:&lt;br /&gt;
               int b;&lt;br /&gt;
        protected:&lt;br /&gt;
               int c;&lt;br /&gt;
};&lt;br /&gt;
struct testB : testA {&lt;br /&gt;
  	       void f() {&lt;br /&gt;
    	       a = 1;  // Cannot be accessed since a is private in testA&lt;br /&gt;
               b = 2;&lt;br /&gt;
    	       c = 3;&lt;br /&gt;
  	       }&lt;br /&gt;
};&lt;br /&gt;
struct testC {&lt;br /&gt;
  	void f(testA x) {&lt;br /&gt;
    		x.a = 4;&lt;br /&gt;
                x.b = 5;&lt;br /&gt;
    	        x.c = 6;&lt;br /&gt;
  		}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int main() {&lt;br /&gt;
  	testA y;&lt;br /&gt;
        y.a = 7;  // Cannot be accessed since a is private in testA&lt;br /&gt;
        y.b = 8;&lt;br /&gt;
        y.c = 9; // Cannot be accessed since c is protected in testA&lt;br /&gt;
&lt;br /&gt;
        testB z;&lt;br /&gt;
        z.a = 10; // Cannot be accessed since a is private in testA&lt;br /&gt;
        z.b = 11;&lt;br /&gt;
        z.c = 12; // Cannot be accessed since c is protected in testA&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
C++ also provides access specifiers for derived classes.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Base_Class&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    int Public_Func();    // Declare a public member.&lt;br /&gt;
protected:&lt;br /&gt;
    int Protected_Func(); // Declare a protected member.&lt;br /&gt;
private:&lt;br /&gt;
    int Private_Func();   // Declare a private member.&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Declare two classes derived from BaseClass.&lt;br /&gt;
class Derived_Class1 : public BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Derived_Class2 : private BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Derived_Class1, the member function Public_Func is a public member and Protected_Func is a protected member because Base_Class is a public base class. Private_Func is private to Base_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
In Derived_Class2, the functions Public_Func and Protected_Func are considered private members because Base_Class is a private base class. Again, Private_Func is private toBase_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] provides 4 types of access specifiers Public, Private, Protected and default.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Situation&lt;br /&gt;
! Public&lt;br /&gt;
! Protected&lt;br /&gt;
! default&lt;br /&gt;
! private&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from same Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from different Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no,unless it is&lt;br /&gt;
a subclass&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Public methods, variables and classes can be accessed from anywhere in the file system. The constraint that there should be just one public class per file is enforced.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square &lt;br /&gt;
{ 	 // public class&lt;br /&gt;
  public x, y;   // public instance variables&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Private variables and methods has only class level access. Not even subclasses can access those. Such kind off variables can be accessed by getter and setter methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square&lt;br /&gt;
 {   // public class&lt;br /&gt;
  	private double x, y;   // private (encapsulated) instance variables&lt;br /&gt;
&lt;br /&gt;
  	public setCorner(int x, int y) &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		this.x = x;&lt;br /&gt;
    		this.y = y;&lt;br /&gt;
  	}&lt;br /&gt;
&lt;br /&gt;
  	public getCorner() &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		return Point(x, y);&lt;br /&gt;
  	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Protected methods and variables can be accessed by the same class, its sub classes and those within the same package. While in Default the class, methods and fields have only package level access. Below illustrates an example on Protected and Private access specifiers.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class calculate&lt;br /&gt;
{&lt;br /&gt;
	protected double x,y;&lt;br /&gt;
	private double z;&lt;br /&gt;
&lt;br /&gt;
	calculate(double x, double y)&lt;br /&gt;
	{&lt;br /&gt;
		this.x = x;&lt;br /&gt;
		this.y = y;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class multiplication extends calculate&lt;br /&gt;
{&lt;br /&gt;
	void prod()&lt;br /&gt;
	{&lt;br /&gt;
		double result = x * y;&lt;br /&gt;
		// z = result;  // Since z is private&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby ===&lt;br /&gt;
The concept of private,protected and public methods in [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] is somewhere different than it is in languages like Java(the public concept is similar).&lt;br /&gt;
&lt;br /&gt;
Private&lt;br /&gt;
&lt;br /&gt;
In Ruby when a method is declared as private,it means it can never be called with an explicit receiver. This typically means that we can call a private method from within a class it is declared as well as subclasses of this class.Below illustrates an example for private access specifier.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  private&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
A.new.main_method&lt;br /&gt;
B.new.main_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, as soon as we try to use an explicit receiver, even if the receiver is &amp;quot;self&amp;quot;, the method call will fail .Below illustrates an example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
C.new.main_method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
a.rb:36:in `main_method': private method `method1' called for #&amp;lt;C:0x7f67025a0648&amp;gt; (NoMethodError)&lt;br /&gt;
    from a.rb:40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see that a private method can never be called from outside the class hierarchy where it is defined.&lt;br /&gt;
&lt;br /&gt;
Protected methods can be called with an implicit receiver,just like a private method,but in addition to this it can be called with an explicit receiver as long as this receiver is self.Below illustrates an example of this:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  protected&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
hello from C&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see here unlike the private method ,call with implicit receiver in class B and explicit receiver in class C both succeed.&lt;br /&gt;
&lt;br /&gt;
Public methods are accessible with any kind of explicit or implicit receiver from anywhere.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In OO languages one of the key principle is encapsulation/data hiding. The examples of different OO programming languages have been illustrated above and how access control helps in achieving encapsulation. With the progress of each OO language the access specifiers definition have been formulated to suit the behaviour of the language.&lt;br /&gt;
&lt;br /&gt;
In java 1.0 release there was the private protected specifier. It had the same behavior of the protected modifier in C++, keeping access to subclasses, but not package members. It didn't fall into the nice progression of adding access, going from private to default to protected to public, as it was more like going from private to private protected to public - bypassing default all together and was hence dropped.&lt;br /&gt;
&lt;br /&gt;
Thus access specifiers are an important concept in OO languages and their importance and need should be understood by programmer's for better progrmming.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1]http://msdn.microsoft.com/en-us/library/7f45fat0(v=vs.80).aspx&lt;br /&gt;
&lt;br /&gt;
[2]http://staff.science.uva.nl/~heck/JAVAcourse/ch4/ss2_2.html&lt;br /&gt;
&lt;br /&gt;
[3]http://en.wikipedia.org/wiki/Class_(computer_programming)&lt;br /&gt;
&lt;br /&gt;
[4]http://www.skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/&lt;br /&gt;
&lt;br /&gt;
[5]http://www.jvoegele.com/software/langcomp.html&lt;br /&gt;
&lt;br /&gt;
[6]http://www.jot.fm/issues/issue_2005_05/article3/&lt;br /&gt;
&lt;br /&gt;
[7]http://www.jguru.com/faq/view.jsp?EID=15576&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50252</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b jp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50252"/>
		<updated>2011-09-22T04:44:47Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Access control is defined as the ability for a modules implementation to remain hidden behind its public interface. This is achieved in object oriented languages via encapsulation. Access control is closely related to the encapsulation/information hiding principle of object-oriented languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The term “object oriented programming” was first used by Xerox PARC in [http://en.wikipedia.org/wiki/Smalltalk_programming_language Smalltalk programming language] so as to refer to the process of using objects as the foundation for computation. Since the introduction of OOP, a large number of modern programming languages are now using the concept. Some of these are [http://en.wikipedia.org/wiki/Fortran FORTRAN],[http://en.wikipedia.org/wiki/BASIC BASIC], and [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal]. There have been some compatibility issues, because many programs were not designed with a OOPs approach in mind.&lt;br /&gt;
To solve this researcher’s suggested designing languages that used OOP concepts and still retained many of the functions that programmers needed. Examples of this type are Eiffel, Java, and Ruby.&lt;br /&gt;
&lt;br /&gt;
== Need for Access Control In OO Languages ==&lt;br /&gt;
&lt;br /&gt;
One of the key OOP’s concept considered while designing programming languages is encapsulation. Encapsulation will allow a class to hide information from objects that may use the code. For example, the Cat class will have a purr() method. A code will be written which will explain how the cat purrs. Despite this, it is not necessary for Betsy the cat to know how she purrs. Encapsulation makes it easier for the developer to change the code without having to change the code for Betsy. When the structure of class is hidden, it is harder for it to be used in the wrong way. &lt;br /&gt;
&lt;br /&gt;
Encapsulation will basically state which class is allowed to use the members of a certain object. This concept makes it easier for programmers to deal with or avoid errors when developing a program. The members within OOP can be categorized as being '''protected, public, or private.'''&lt;br /&gt;
&lt;br /&gt;
== Access Control in various OO Languagues ==&lt;br /&gt;
&lt;br /&gt;
 	&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! &lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
|-&lt;br /&gt;
| Access &lt;br /&gt;
control&lt;br /&gt;
&lt;br /&gt;
|Public,protected,private,”friends&lt;br /&gt;
&lt;br /&gt;
|public,private,package,protected&lt;br /&gt;
&lt;br /&gt;
|private,public,protected&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
In [http://en.wikipedia.org/wiki/C%2B%2B C++] we have private, public and protected. Suppose variable “data” is a member of class stack and is private then it can accessed by members and friends of stack class. If the member is public then it has no access restrictions. But if the member is protected then it can be accessed by members and friends of stack class and those classes derived from stack class. The members in class are private by default and those of struct/union are public by default. The example below explains the access specifiers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
struct testA {&lt;br /&gt;
  	friend class testC;&lt;br /&gt;
        private:&lt;br /&gt;
               int a;&lt;br /&gt;
        public:&lt;br /&gt;
               int b;&lt;br /&gt;
        protected:&lt;br /&gt;
               int c;&lt;br /&gt;
};&lt;br /&gt;
struct testB : testA {&lt;br /&gt;
  	       void f() {&lt;br /&gt;
    	       a = 1;  // Cannot be accessed since a is private in testA&lt;br /&gt;
               b = 2;&lt;br /&gt;
    	       c = 3;&lt;br /&gt;
  	       }&lt;br /&gt;
};&lt;br /&gt;
struct testC {&lt;br /&gt;
  	void f(testA x) {&lt;br /&gt;
    		x.a = 4;&lt;br /&gt;
                x.b = 5;&lt;br /&gt;
    	        x.c = 6;&lt;br /&gt;
  		}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int main() {&lt;br /&gt;
  	testA y;&lt;br /&gt;
        y.a = 7;  // Cannot be accessed since a is private in testA&lt;br /&gt;
        y.b = 8;&lt;br /&gt;
        y.c = 9; // Cannot be accessed since c is protected in testA&lt;br /&gt;
&lt;br /&gt;
        testB z;&lt;br /&gt;
        z.a = 10; // Cannot be accessed since a is private in testA&lt;br /&gt;
        z.b = 11;&lt;br /&gt;
        z.c = 12; // Cannot be accessed since c is protected in testA&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
C++ also provides access specifiers for derived classes.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Base_Class&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    int Public_Func();    // Declare a public member.&lt;br /&gt;
protected:&lt;br /&gt;
    int Protected_Func(); // Declare a protected member.&lt;br /&gt;
private:&lt;br /&gt;
    int Private_Func();   // Declare a private member.&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Declare two classes derived from BaseClass.&lt;br /&gt;
class Derived_Class1 : public BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Derived_Class2 : private BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Derived_Class1, the member function Public_Func is a public member and Protected_Func is a protected member because Base_Class is a public base class. Private_Func is private to Base_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
In Derived_Class2, the functions Public_Func and Protected_Func are considered private members because Base_Class is a private base class. Again, Private_Func is private toBase_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] provides 4 types of access specifiers Public, Private, Protected and default.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Situation&lt;br /&gt;
! Public&lt;br /&gt;
! Protected&lt;br /&gt;
! default&lt;br /&gt;
! private&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from same Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from different Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no,unless it is&lt;br /&gt;
a subclass&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Public methods, variables and classes can be accessed from anywhere in the file system. The constraint that there should be just one public class per file is enforced.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square &lt;br /&gt;
{ 	 // public class&lt;br /&gt;
  public x, y;   // public instance variables&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Private variables and methods has only class level access. Not even subclasses can access those. Such kind off variables can be accessed by getter and setter methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square&lt;br /&gt;
 {   // public class&lt;br /&gt;
  	private double x, y;   // private (encapsulated) instance variables&lt;br /&gt;
&lt;br /&gt;
  	public setCorner(int x, int y) &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		this.x = x;&lt;br /&gt;
    		this.y = y;&lt;br /&gt;
  	}&lt;br /&gt;
&lt;br /&gt;
  	public getCorner() &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		return Point(x, y);&lt;br /&gt;
  	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Protected methods and variables can be accessed by the same class, its sub classes and those within the same package. While in Default the class, methods and fields have only package level access. Below illustrates an example on Protected and Private access specifiers.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class calculate&lt;br /&gt;
{&lt;br /&gt;
	protected double x,y;&lt;br /&gt;
	private double z;&lt;br /&gt;
&lt;br /&gt;
	calculate(double x, double y)&lt;br /&gt;
	{&lt;br /&gt;
		this.x = x;&lt;br /&gt;
		this.y = y;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class multiplication extends calculate&lt;br /&gt;
{&lt;br /&gt;
	void prod()&lt;br /&gt;
	{&lt;br /&gt;
		double result = x * y;&lt;br /&gt;
		// z = result;  // Since z is private&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby ===&lt;br /&gt;
The concept of private,protected and public methods in [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] is somewhere different than it is in languages like Java(the public concept is similar).&lt;br /&gt;
&lt;br /&gt;
Private&lt;br /&gt;
&lt;br /&gt;
In Ruby when a method is declared as private,it means it can never be called with an explicit receiver. This typically means that we can call a private method from within a class it is declared as well as subclasses of this class.Below illustrates an example for private access specifier.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  private&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
A.new.main_method&lt;br /&gt;
B.new.main_method&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, as soon as we try to use an explicit receiver, even if the receiver is &amp;quot;self&amp;quot;, the method call will fail .Below illustrates an example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
C.new.main_method&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
a.rb:36:in `main_method': private method `method1' called for #&amp;lt;C:0x7f67025a0648&amp;gt; (NoMethodError)&lt;br /&gt;
    from a.rb:40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus we see that a private method can never be called from outside the class hierarchy where it is defined.&lt;br /&gt;
&lt;br /&gt;
Protected methods can be called with an implicit receiver,just like a private method,but in addition to this it can be called with an explicit receiver as long as this receiver is self.Below illustrates an example of this:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  protected&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
hello from C&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see here unlike the private method ,call with implicit receiver in class B and explicit receiver in class C both succeed.&lt;br /&gt;
&lt;br /&gt;
Public methods are accessible with any kind of explicit or implicit receiver from anywhere.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In OO languages one of the key principle is encapsulation/data hiding. The examples of different OO programming languages have been illustrated above and how access control helps in achieving encapsulation. With the progress of each OO language the access specifiers definition have been formulated to suit the behaviour of the language.&lt;br /&gt;
&lt;br /&gt;
In java 1.0 release there was the private protected specifier. It had the same behavior of the protected modifier in C++, keeping access to subclasses, but not package members. It didn't fall into the nice progression of adding access, going from private to default to protected to public, as it was more like going from private to private protected to public - bypassing default all together and was hence dropped.&lt;br /&gt;
&lt;br /&gt;
Thus access specifiers are an important concept in OO languages and their importance and need should be understood by programmer's for better progrmming.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1]http://msdn.microsoft.com/en-us/library/7f45fat0(v=vs.80).aspx&lt;br /&gt;
&lt;br /&gt;
[2]http://staff.science.uva.nl/~heck/JAVAcourse/ch4/ss2_2.html&lt;br /&gt;
&lt;br /&gt;
[3]http://en.wikipedia.org/wiki/Class_(computer_programming)&lt;br /&gt;
&lt;br /&gt;
[4]http://www.skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/&lt;br /&gt;
&lt;br /&gt;
[5]http://www.jvoegele.com/software/langcomp.html&lt;br /&gt;
&lt;br /&gt;
[6]http://www.jot.fm/issues/issue_2005_05/article3/&lt;br /&gt;
&lt;br /&gt;
[7]http://www.jguru.com/faq/view.jsp?EID=15576&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50249</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b jp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50249"/>
		<updated>2011-09-22T04:42:52Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Access control is defined as the ability for a modules implementation to remain hidden behind its public interface. This is achieved in object oriented languages via encapsulation. Access control is closely related to the encapsulation/information hiding principle of object-oriented languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The term “object oriented programming” was first used by Xerox PARC in [http://en.wikipedia.org/wiki/Smalltalk_programming_language Smalltalk programming language] so as to refer to the process of using objects as the foundation for computation. Since the introduction of OOP, a large number of modern programming languages are now using the concept. Some of these are [http://en.wikipedia.org/wiki/Fortran FORTRAN],[http://en.wikipedia.org/wiki/BASIC BASIC], and [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal]. There have been some compatibility issues, because many programs were not designed with a OOPs approach in mind.&lt;br /&gt;
To solve this researcher’s suggested designing languages that used OOP concepts and still retained many of the functions that programmers needed. Examples of this type are Eiffel, Java, and Ruby.&lt;br /&gt;
&lt;br /&gt;
== Need for Access Control In OO Languages ==&lt;br /&gt;
&lt;br /&gt;
One of the key OOP’s concept considered while designing programming languages is encapsulation. Encapsulation will allow a class to hide information from objects that may use the code. For example, the Cat class will have a purr() method. A code will be written which will explain how the cat purrs. Despite this, it is not necessary for Betsy the cat to know how she purrs. Encapsulation makes it easier for the developer to change the code without having to change the code for Betsy. When the structure of class is hidden, it is harder for it to be used in the wrong way. &lt;br /&gt;
&lt;br /&gt;
Encapsulation will basically state which class is allowed to use the members of a certain object. This concept makes it easier for programmers to deal with or avoid errors when developing a program. The members within OOP can be categorized as being '''protected, public, or private.'''&lt;br /&gt;
&lt;br /&gt;
== Access Control in various OO Languagues ==&lt;br /&gt;
&lt;br /&gt;
 	&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! &lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
|-&lt;br /&gt;
| Access &lt;br /&gt;
control&lt;br /&gt;
&lt;br /&gt;
|Public,protected,private,”friends&lt;br /&gt;
&lt;br /&gt;
|public,private,package,protected&lt;br /&gt;
&lt;br /&gt;
|private,public,protected&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
In [http://en.wikipedia.org/wiki/C%2B%2B C++] we have private, public and protected. Suppose variable “data” is a member of class stack and is private then it can accessed by members and friends of stack class. If the member is public then it has no access restrictions. But if the member is protected then it can be accessed by members and friends of stack class and those classes derived from stack class. The members in class are private by default and those of struct/union are public by default. The example below explains the access specifiers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
struct testA {&lt;br /&gt;
  	friend class testC;&lt;br /&gt;
        private:&lt;br /&gt;
               int a;&lt;br /&gt;
        public:&lt;br /&gt;
               int b;&lt;br /&gt;
        protected:&lt;br /&gt;
               int c;&lt;br /&gt;
};&lt;br /&gt;
struct testB : testA {&lt;br /&gt;
  	       void f() {&lt;br /&gt;
    	       a = 1;  // Cannot be accessed since a is private in testA&lt;br /&gt;
               b = 2;&lt;br /&gt;
    	       c = 3;&lt;br /&gt;
  	       }&lt;br /&gt;
};&lt;br /&gt;
struct testC {&lt;br /&gt;
  	void f(testA x) {&lt;br /&gt;
    		x.a = 4;&lt;br /&gt;
                x.b = 5;&lt;br /&gt;
    	        x.c = 6;&lt;br /&gt;
  		}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int main() {&lt;br /&gt;
  	testA y;&lt;br /&gt;
        y.a = 7;  // Cannot be accessed since a is private in testA&lt;br /&gt;
        y.b = 8;&lt;br /&gt;
        y.c = 9; // Cannot be accessed since c is protected in testA&lt;br /&gt;
&lt;br /&gt;
        testB z;&lt;br /&gt;
        z.a = 10; // Cannot be accessed since a is private in testA&lt;br /&gt;
        z.b = 11;&lt;br /&gt;
        z.c = 12; // Cannot be accessed since c is protected in testA&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
C++ also provides access specifiers for derived classes.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Base_Class&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    int Public_Func();    // Declare a public member.&lt;br /&gt;
protected:&lt;br /&gt;
    int Protected_Func(); // Declare a protected member.&lt;br /&gt;
private:&lt;br /&gt;
    int Private_Func();   // Declare a private member.&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Declare two classes derived from BaseClass.&lt;br /&gt;
class Derived_Class1 : public BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Derived_Class2 : private BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Derived_Class1, the member function Public_Func is a public member and Protected_Func is a protected member because Base_Class is a public base class. Private_Func is private to Base_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
In Derived_Class2, the functions Public_Func and Protected_Func are considered private members because Base_Class is a private base class. Again, Private_Func is private toBase_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] provides 4 types of access specifiers Public, Private, Protected and default.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Situation&lt;br /&gt;
! Public&lt;br /&gt;
! Protected&lt;br /&gt;
! default&lt;br /&gt;
! private&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from same Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from different Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no,unless it is&lt;br /&gt;
a subclass&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Public methods, variables and classes can be accessed from anywhere in the file system. The constraint that there should be just one public class per file is enforced.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square &lt;br /&gt;
{ 	 // public class&lt;br /&gt;
  public x, y;   // public instance variables&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Private variables and methods has only class level access. Not even subclasses can access those. Such kind off variables can be accessed by getter and setter methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square&lt;br /&gt;
 {   // public class&lt;br /&gt;
  	private double x, y;   // private (encapsulated) instance variables&lt;br /&gt;
&lt;br /&gt;
  	public setCorner(int x, int y) &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		this.x = x;&lt;br /&gt;
    		this.y = y;&lt;br /&gt;
  	}&lt;br /&gt;
&lt;br /&gt;
  	public getCorner() &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		return Point(x, y);&lt;br /&gt;
  	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Protected methods and variables can be accessed by the same class, its sub classes and those within the same package. While in Default the class, methods and fields have only package level access. Below illustrates an example on Protected and Private access specifiers.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class calculate&lt;br /&gt;
{&lt;br /&gt;
	protected double x,y;&lt;br /&gt;
	private double z;&lt;br /&gt;
&lt;br /&gt;
	calculate(double x, double y)&lt;br /&gt;
	{&lt;br /&gt;
		this.x = x;&lt;br /&gt;
		this.y = y;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class multiplication extends calculate&lt;br /&gt;
{&lt;br /&gt;
	void prod()&lt;br /&gt;
	{&lt;br /&gt;
		double result = x * y;&lt;br /&gt;
		// z = result;  // Since z is private&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby ===&lt;br /&gt;
The concept of private,protected and public methods in [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]is somewhere different than it is in languages like Java(the public concept is similar).&lt;br /&gt;
&lt;br /&gt;
Private&lt;br /&gt;
&lt;br /&gt;
In Ruby when a method is declared as private,it means it can never be called with an explicit receiver. This typically means that we can call a private method from within a class it is declared as well as subclasses of this class.Below illustrates an example for private access specifier.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  private&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
A.new.main_method&lt;br /&gt;
B.new.main_method&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, as soon as we try to use an explicit receiver, even if the receiver is &amp;quot;self&amp;quot;, the method call will fail .Below illustrates an example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
C.new.main_method&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
a.rb:36:in `main_method': private method `method1' called for #&amp;lt;C:0x7f67025a0648&amp;gt; (NoMethodError)&lt;br /&gt;
    from a.rb:40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus we see that a private method can never be called from outside the class hierarchy where it is defined.&lt;br /&gt;
&lt;br /&gt;
Protected methods can be called with an implicit receiver,just like a private method,but in addition to this it can be called with an explicit receiver as long as this receiver is self.Below illustrates an example of this:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  protected&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
hello from C&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see here unlike the private method ,call with implicit receiver in class B and explicit receiver in class C both succeed.&lt;br /&gt;
&lt;br /&gt;
Public methods are accessible with any kind of explicit or implicit receiver from anywhere.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In OO languages one of the key principle is encapsulation/data hiding. The examples of different OO programming languages have been illustrated above and how access control helps in achieving encapsulation. With the progress of each OO language the access specifiers definition have been formulated to suit the behaviour of the language.&lt;br /&gt;
&lt;br /&gt;
In java 1.0 release there was the private protected specifier. It had the same behavior of the protected modifier in C++, keeping access to subclasses, but not package members. It didn't fall into the nice progression of adding access, going from private to default to protected to public, as it was more like going from private to private protected to public - bypassing default all together and was hence dropped.&lt;br /&gt;
&lt;br /&gt;
Thus access specifiers are an important concept in OO languages and their importance and need should be understood by programmer's for better progrmming.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1]http://msdn.microsoft.com/en-us/library/7f45fat0(v=vs.80).aspx&lt;br /&gt;
&lt;br /&gt;
[2]http://staff.science.uva.nl/~heck/JAVAcourse/ch4/ss2_2.html&lt;br /&gt;
&lt;br /&gt;
[3]http://en.wikipedia.org/wiki/Class_(computer_programming)&lt;br /&gt;
&lt;br /&gt;
[4]http://www.skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/&lt;br /&gt;
&lt;br /&gt;
[5]http://www.jvoegele.com/software/langcomp.html&lt;br /&gt;
&lt;br /&gt;
[6]http://www.jot.fm/issues/issue_2005_05/article3/&lt;br /&gt;
&lt;br /&gt;
[7]http://www.jguru.com/faq/view.jsp?EID=15576&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50247</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b jp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50247"/>
		<updated>2011-09-22T04:40:51Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Need for Access Control In OO Languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Access control is defined as the ability for a modules implementation to remain hidden behind its public interface. This is achieved in object oriented languages via encapsulation. Access control is closely related to the encapsulation/information hiding principle of object-oriented languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The term “object oriented programming” was first used by Xerox PARC in [http://en.wikipedia.org/wiki/Smalltalk_programming_language Smalltalk programming language] so as to refer to the process of using objects as the foundation for computation. Since the introduction of OOP, a large number of modern programming languages are now using the concept. Some of these are [http://en.wikipedia.org/wiki/Fortran FORTRAN],[http://en.wikipedia.org/wiki/BASIC BASIC], and [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal]. There have been some compatibility issues, because many programs were not designed with a OOPs approach in mind.&lt;br /&gt;
To solve this researcher’s suggested designing languages that used OOP concepts and still retained many of the functions that programmers needed. Examples of this type are Eiffel, Java, and Ruby.&lt;br /&gt;
&lt;br /&gt;
== Need for Access Control In OO Languages ==&lt;br /&gt;
&lt;br /&gt;
One of the key OOP’s concept considered while designing programming languages is encapsulation. Encapsulation will allow a class to hide information from objects that may use the code. For example, the Cat class will have a purr() method. A code will be written which will explain how the cat purrs. Despite this, it is not necessary for Betsy the cat to know how she purrs. Encapsulation makes it easier for the developer to change the code without having to change the code for Betsy. When the structure of class is hidden, it is harder for it to be used in the wrong way. &lt;br /&gt;
&lt;br /&gt;
Encapsulation will basically state which class is allowed to use the members of a certain object. This concept makes it easier for programmers to deal with or avoid errors when developing a program. The members within OOP can be categorized as being '''protected, public, or private.'''&lt;br /&gt;
&lt;br /&gt;
== Access Control in various OO Languagues ==&lt;br /&gt;
&lt;br /&gt;
 	&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! &lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
|-&lt;br /&gt;
| Access &lt;br /&gt;
control&lt;br /&gt;
&lt;br /&gt;
|Public,protected,private,”friends&lt;br /&gt;
&lt;br /&gt;
|public,private,package,protected&lt;br /&gt;
&lt;br /&gt;
|private,public,protected&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
In [http://en.wikipedia.org/wiki/C%2B%2B C++] we have private, public and protected. Suppose variable “data” is a member of class stack and is private then it can accessed by members and friends of stack class. If the member is public then it has no access restrictions. But if the member is protected then it can be accessed by members and friends of stack class and those classes derived from stack class. The members in class are private by default and those of struct/union are public by default. The example below explains the access specifiers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
struct testA {&lt;br /&gt;
  	friend class testC;&lt;br /&gt;
        private:&lt;br /&gt;
               int a;&lt;br /&gt;
        public:&lt;br /&gt;
               int b;&lt;br /&gt;
        protected:&lt;br /&gt;
               int c;&lt;br /&gt;
};&lt;br /&gt;
struct testB : testA {&lt;br /&gt;
  	void f() {&lt;br /&gt;
    	a = 1;  // Cannot be accessed since a is private in testA&lt;br /&gt;
       	b = 2;&lt;br /&gt;
    	c = 3;&lt;br /&gt;
  	}&lt;br /&gt;
};&lt;br /&gt;
struct testC {&lt;br /&gt;
  	void f(testA x) {&lt;br /&gt;
    		x.a = 4;&lt;br /&gt;
                x.b = 5;&lt;br /&gt;
    	        x.c = 6;&lt;br /&gt;
  		}&lt;br /&gt;
};&lt;br /&gt;
int main() {&lt;br /&gt;
  	testA y;&lt;br /&gt;
        y.a = 7;  // Cannot be accessed since a is private in testA&lt;br /&gt;
        y.b = 8;&lt;br /&gt;
        y.c = 9; // Cannot be accessed since c is protected in testA&lt;br /&gt;
&lt;br /&gt;
        testB z;&lt;br /&gt;
        z.a = 10; // Cannot be accessed since a is private in testA&lt;br /&gt;
        z.b = 11;&lt;br /&gt;
        z.c = 12; // Cannot be accessed since c is protected in testA&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
C++ also provides access specifiers for derived classes.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Base_Class&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    int Public_Func();    // Declare a public member.&lt;br /&gt;
protected:&lt;br /&gt;
    int Protected_Func(); // Declare a protected member.&lt;br /&gt;
private:&lt;br /&gt;
    int Private_Func();   // Declare a private member.&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Declare two classes derived from BaseClass.&lt;br /&gt;
class Derived_Class1 : public BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Derived_Class2 : private BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Derived_Class1, the member function Public_Func is a public member and Protected_Func is a protected member because Base_Class is a public base class. Private_Func is private to Base_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
In Derived_Class2, the functions Public_Func and Protected_Func are considered private members because Base_Class is a private base class. Again, Private_Func is private toBase_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] provides 4 types of access specifiers Public, Private, Protected and default.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Situation&lt;br /&gt;
! Public&lt;br /&gt;
! Protected&lt;br /&gt;
! default&lt;br /&gt;
! private&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from same Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from different Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no,unless it is&lt;br /&gt;
a subclass&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Public methods, variables and classes can be accessed from anywhere in the file system. The constraint that there should be just one public class per file is enforced.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square &lt;br /&gt;
{ 	 // public class&lt;br /&gt;
  public x, y;   // public instance variables&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Private variables and methods has only class level access. Not even subclasses can access those. Such kind off variables can be accessed by getter and setter methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square&lt;br /&gt;
 {   // public class&lt;br /&gt;
  	private double x, y;   // private (encapsulated) instance variables&lt;br /&gt;
&lt;br /&gt;
  	public setCorner(int x, int y) &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		this.x = x;&lt;br /&gt;
    		this.y = y;&lt;br /&gt;
  	}&lt;br /&gt;
&lt;br /&gt;
  	public getCorner() &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		return Point(x, y);&lt;br /&gt;
  	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Protected methods and variables can be accessed by the same class, its sub classes and those within the same package. While in Default the class, methods and fields have only package level access. Below illustrates an example on Protected and Private access specifiers.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class calculate&lt;br /&gt;
{&lt;br /&gt;
	protected double x,y;&lt;br /&gt;
	private double z;&lt;br /&gt;
&lt;br /&gt;
	calculate(double x, double y)&lt;br /&gt;
	{&lt;br /&gt;
		this.x = x;&lt;br /&gt;
		this.y = y;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class multiplication extends calculate&lt;br /&gt;
{&lt;br /&gt;
	void prod()&lt;br /&gt;
	{&lt;br /&gt;
		double result = x * y;&lt;br /&gt;
		// z = result;  // Since z is private&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby ===&lt;br /&gt;
The concept of private,protected and public methods in [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]is somewhere different than it is in languages like Java(the public concept is similar).&lt;br /&gt;
&lt;br /&gt;
Private&lt;br /&gt;
&lt;br /&gt;
In Ruby when a method is declared as private,it means it can never be called with an explicit receiver. This typically means that we can call a private method from within a class it is declared as well as subclasses of this class.Below illustrates an example for private access specifier.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  private&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
A.new.main_method&lt;br /&gt;
B.new.main_method&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, as soon as we try to use an explicit receiver, even if the receiver is &amp;quot;self&amp;quot;, the method call will fail .Below illustrates an example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
C.new.main_method&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
a.rb:36:in `main_method': private method `method1' called for #&amp;lt;C:0x7f67025a0648&amp;gt; (NoMethodError)&lt;br /&gt;
    from a.rb:40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus we see that a private method can never be called from outside the class hierarchy where it is defined.&lt;br /&gt;
&lt;br /&gt;
Protected methods can be called with an implicit receiver,just like a private method,but in addition to this it can be called with an explicit receiver as long as this receiver is self.Below illustrates an example of this:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  protected&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
hello from C&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see here unlike the private method ,call with implicit receiver in class B and explicit receiver in class C both succeed.&lt;br /&gt;
&lt;br /&gt;
Public methods are accessible with any kind of explicit or implicit receiver from anywhere.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In OO languages one of the key principle is encapsulation/data hiding. The examples of different OO programming languages have been illustrated above and how access control helps in achieving encapsulation. With the progress of each OO language the access specifiers definition have been formulated to suit the behaviour of the language.&lt;br /&gt;
&lt;br /&gt;
In java 1.0 release there was the private protected specifier. It had the same behavior of the protected modifier in C++, keeping access to subclasses, but not package members. It didn't fall into the nice progression of adding access, going from private to default to protected to public, as it was more like going from private to private protected to public - bypassing default all together and was hence dropped.&lt;br /&gt;
&lt;br /&gt;
Thus access specifiers are an important concept in OO languages and their importance and need should be understood by programmer's for better progrmming.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1]http://msdn.microsoft.com/en-us/library/7f45fat0(v=vs.80).aspx&lt;br /&gt;
&lt;br /&gt;
[2]http://staff.science.uva.nl/~heck/JAVAcourse/ch4/ss2_2.html&lt;br /&gt;
&lt;br /&gt;
[3]http://en.wikipedia.org/wiki/Class_(computer_programming)&lt;br /&gt;
&lt;br /&gt;
[4]http://www.skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/&lt;br /&gt;
&lt;br /&gt;
[5]http://www.jvoegele.com/software/langcomp.html&lt;br /&gt;
&lt;br /&gt;
[6]http://www.jot.fm/issues/issue_2005_05/article3/&lt;br /&gt;
&lt;br /&gt;
[7]http://www.jguru.com/faq/view.jsp?EID=15576&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50242</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b jp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=50242"/>
		<updated>2011-09-22T04:36:47Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Access control is defined as the ability for a modules implementation to remain hidden behind its public interface. This is achieved in object oriented languages via encapsulation. Access control is closely related to the encapsulation/information hiding principle of object-oriented languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The term “object oriented programming” was first used by Xerox PARC in [http://en.wikipedia.org/wiki/Smalltalk_programming_language Smalltalk programming language] so as to refer to the process of using objects as the foundation for computation. Since the introduction of OOP, a large number of modern programming languages are now using the concept. Some of these are [http://en.wikipedia.org/wiki/Fortran FORTRAN],[http://en.wikipedia.org/wiki/BASIC BASIC], and [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal]. There have been some compatibility issues, because many programs were not designed with a OOPs approach in mind.&lt;br /&gt;
To solve this researcher’s suggested designing languages that used OOP concepts and still retained many of the functions that programmers needed. Examples of this type are Eiffel, Java, and Ruby.&lt;br /&gt;
&lt;br /&gt;
== Need for Access Control In OO Languages ==&lt;br /&gt;
&lt;br /&gt;
One of the key OOP’s concept considered while designing programming languages is encapsulation. Encapsulation will allow a class to hide information from objects that may use the code. For example, the Cat class will have a purr() method. A code will be written which will explain how the cat purrs. Despite this, it is not necessary for Betsy the cat to know how she purrs. Encapsulation makes it easier for the developer to change the code without having to change the code for Betsy. When the structure of class is hidden, it is harder for it to be used in the wrong way. &lt;br /&gt;
&lt;br /&gt;
Encapsulation will basically state which class is allowed to use the members of a certain object. This concept makes it easier for programmers to deal with or avoid errors when developing a program. The members within OOP can be categorized as being protected, public, or private.&lt;br /&gt;
&lt;br /&gt;
== Access Control in various OO Languagues ==&lt;br /&gt;
&lt;br /&gt;
 	&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! &lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
|-&lt;br /&gt;
| Access &lt;br /&gt;
control&lt;br /&gt;
&lt;br /&gt;
|Public,protected,private,”friends&lt;br /&gt;
&lt;br /&gt;
|public,private,package,protected&lt;br /&gt;
&lt;br /&gt;
|private,public,protected&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
In [http://en.wikipedia.org/wiki/C%2B%2B C++] we have private, public and protected. Suppose variable “data” is a member of class stack and is private then it can accessed by members and friends of stack class. If the member is public then it has no access restrictions. But if the member is protected then it can be accessed by members and friends of stack class and those classes derived from stack class. The members in class are private by default and those of struct/union are public by default. The example below explains the access specifiers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
struct testA {&lt;br /&gt;
  	friend class testC;&lt;br /&gt;
        private:&lt;br /&gt;
               int a;&lt;br /&gt;
        public:&lt;br /&gt;
               int b;&lt;br /&gt;
        protected:&lt;br /&gt;
               int c;&lt;br /&gt;
};&lt;br /&gt;
struct testB : testA {&lt;br /&gt;
  	void f() {&lt;br /&gt;
    	a = 1;  // Cannot be accessed since a is private in testA&lt;br /&gt;
       	b = 2;&lt;br /&gt;
    	c = 3;&lt;br /&gt;
  	}&lt;br /&gt;
};&lt;br /&gt;
struct testC {&lt;br /&gt;
  	void f(testA x) {&lt;br /&gt;
    		x.a = 4;&lt;br /&gt;
                x.b = 5;&lt;br /&gt;
    	        x.c = 6;&lt;br /&gt;
  		}&lt;br /&gt;
};&lt;br /&gt;
int main() {&lt;br /&gt;
  	testA y;&lt;br /&gt;
        y.a = 7;  // Cannot be accessed since a is private in testA&lt;br /&gt;
        y.b = 8;&lt;br /&gt;
        y.c = 9; // Cannot be accessed since c is protected in testA&lt;br /&gt;
&lt;br /&gt;
        testB z;&lt;br /&gt;
        z.a = 10; // Cannot be accessed since a is private in testA&lt;br /&gt;
        z.b = 11;&lt;br /&gt;
        z.c = 12; // Cannot be accessed since c is protected in testA&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
C++ also provides access specifiers for derived classes.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Base_Class&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    int Public_Func();    // Declare a public member.&lt;br /&gt;
protected:&lt;br /&gt;
    int Protected_Func(); // Declare a protected member.&lt;br /&gt;
private:&lt;br /&gt;
    int Private_Func();   // Declare a private member.&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Declare two classes derived from BaseClass.&lt;br /&gt;
class Derived_Class1 : public BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Derived_Class2 : private BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Derived_Class1, the member function Public_Func is a public member and Protected_Func is a protected member because Base_Class is a public base class. Private_Func is private to Base_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
In Derived_Class2, the functions Public_Func and Protected_Func are considered private members because Base_Class is a private base class. Again, Private_Func is private toBase_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] provides 4 types of access specifiers Public, Private, Protected and default.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Situation&lt;br /&gt;
! Public&lt;br /&gt;
! Protected&lt;br /&gt;
! default&lt;br /&gt;
! private&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from same Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from different Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no,unless it is&lt;br /&gt;
a subclass&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Public methods, variables and classes can be accessed from anywhere in the file system. The constraint that there should be just one public class per file is enforced.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square &lt;br /&gt;
{ 	 // public class&lt;br /&gt;
  public x, y;   // public instance variables&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Private variables and methods has only class level access. Not even subclasses can access those. Such kind off variables can be accessed by getter and setter methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square&lt;br /&gt;
 {   // public class&lt;br /&gt;
  	private double x, y;   // private (encapsulated) instance variables&lt;br /&gt;
&lt;br /&gt;
  	public setCorner(int x, int y) &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		this.x = x;&lt;br /&gt;
    		this.y = y;&lt;br /&gt;
  	}&lt;br /&gt;
&lt;br /&gt;
  	public getCorner() &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		return Point(x, y);&lt;br /&gt;
  	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Protected methods and variables can be accessed by the same class, its sub classes and those within the same package. While in Default the class, methods and fields have only package level access. Below illustrates an example on Protected and Private access specifiers.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class calculate&lt;br /&gt;
{&lt;br /&gt;
	protected double x,y;&lt;br /&gt;
	private double z;&lt;br /&gt;
&lt;br /&gt;
	calculate(double x, double y)&lt;br /&gt;
	{&lt;br /&gt;
		this.x = x;&lt;br /&gt;
		this.y = y;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class multiplication extends calculate&lt;br /&gt;
{&lt;br /&gt;
	void prod()&lt;br /&gt;
	{&lt;br /&gt;
		double result = x * y;&lt;br /&gt;
		// z = result;  // Since z is private&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby ===&lt;br /&gt;
The concept of private,protected and public methods in [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]is somewhere different than it is in languages like Java(the public concept is similar).&lt;br /&gt;
&lt;br /&gt;
Private&lt;br /&gt;
&lt;br /&gt;
In Ruby when a method is declared as private,it means it can never be called with an explicit receiver. This typically means that we can call a private method from within a class it is declared as well as subclasses of this class.Below illustrates an example for private access specifier.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  private&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
A.new.main_method&lt;br /&gt;
B.new.main_method&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, as soon as we try to use an explicit receiver, even if the receiver is &amp;quot;self&amp;quot;, the method call will fail .Below illustrates an example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
C.new.main_method&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
a.rb:36:in `main_method': private method `method1' called for #&amp;lt;C:0x7f67025a0648&amp;gt; (NoMethodError)&lt;br /&gt;
    from a.rb:40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus we see that a private method can never be called from outside the class hierarchy where it is defined.&lt;br /&gt;
&lt;br /&gt;
Protected methods can be called with an implicit receiver,just like a private method,but in addition to this it can be called with an explicit receiver as long as this receiver is self.Below illustrates an example of this:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  protected&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
hello from C&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see here unlike the private method ,call with implicit receiver in class B and explicit receiver in class C both succeed.&lt;br /&gt;
&lt;br /&gt;
Public methods are accessible with any kind of explicit or implicit receiver from anywhere.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In OO languages one of the key principle is encapsulation/data hiding. The examples of different OO programming languages have been illustrated above and how access control helps in achieving encapsulation. With the progress of each OO language the access specifiers definition have been formulated to suit the behaviour of the language.&lt;br /&gt;
&lt;br /&gt;
In java 1.0 release there was the private protected specifier. It had the same behavior of the protected modifier in C++, keeping access to subclasses, but not package members. It didn't fall into the nice progression of adding access, going from private to default to protected to public, as it was more like going from private to private protected to public - bypassing default all together and was hence dropped.&lt;br /&gt;
&lt;br /&gt;
Thus access specifiers are an important concept in OO languages and their importance and need should be understood by programmer's for better progrmming.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1]http://msdn.microsoft.com/en-us/library/7f45fat0(v=vs.80).aspx&lt;br /&gt;
&lt;br /&gt;
[2]http://staff.science.uva.nl/~heck/JAVAcourse/ch4/ss2_2.html&lt;br /&gt;
&lt;br /&gt;
[3]http://en.wikipedia.org/wiki/Class_(computer_programming)&lt;br /&gt;
&lt;br /&gt;
[4]http://www.skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/&lt;br /&gt;
&lt;br /&gt;
[5]http://www.jvoegele.com/software/langcomp.html&lt;br /&gt;
&lt;br /&gt;
[6]http://www.jot.fm/issues/issue_2005_05/article3/&lt;br /&gt;
&lt;br /&gt;
[7]http://www.jguru.com/faq/view.jsp?EID=15576&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=49997</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b jp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=49997"/>
		<updated>2011-09-21T15:53:03Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Access control is defined as the ability for a modules implementation to remain hidden behind its public interface. This is achieved in object oriented languages via encapsulation. Access control is closely related to the encapsulation/information hiding principle of object-oriented languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The term “object oriented programming” was first used by Xerox PARC in Smalltalk programming language so as to refer to the process of using objects as the foundation for computation. Since the introduction of OOP, a large number of modern programming languages are now using the concept. Some of these are [http://en.wikipedia.org/wiki/Fortran FORTRAN],[http://en.wikipedia.org/wiki/BASIC BASIC], and [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal]. There have been some compatibility issues, because many programs were not designed with a OOPs approach in mind.&lt;br /&gt;
To solve this researcher’s suggested designing languages that used OOP concepts and still retained many of the functions that programmers needed. Examples of this type are Eiffel, Java, and Ruby.&lt;br /&gt;
&lt;br /&gt;
== Need for Access Control In OO Languages ==&lt;br /&gt;
&lt;br /&gt;
One of the key OOP’s concept considered while designing programming languages is encapsulation. Encapsulation will allow a class to hide information from objects that may use the code. For example, the Cat class will have a purr() method. A code will be written which will explain how the cat purrs. Despite this, it is not necessary for Betsy the cat to know how she purrs. Encapsulation makes it easier for the developer to change the code without having to change the code for Betsy. When the structure of class is hidden, it is harder for it to be used in the wrong way. &lt;br /&gt;
&lt;br /&gt;
Encapsulation will basically state which class is allowed to use the members of a certain object. This concept makes it easier for programmers to deal with or avoid errors when developing a program. The members within OOP can be categorized as being protected, public, or private.&lt;br /&gt;
&lt;br /&gt;
== Access Control in various OO Languagues ==&lt;br /&gt;
&lt;br /&gt;
 	&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! &lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
|-&lt;br /&gt;
| Access &lt;br /&gt;
control&lt;br /&gt;
&lt;br /&gt;
|Public,protected,private,”friends&lt;br /&gt;
&lt;br /&gt;
|public,private,package,protected&lt;br /&gt;
&lt;br /&gt;
|private,public,protected&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
In [http://en.wikipedia.org/wiki/C%2B%2B C++] we have private, public and protected. Suppose variable “data” is a member of class stack and is private then it can accessed by members and friends of stack class. If the member is public then it has no access restrictions. But if the member is protected then it can be accessed by members and friends of stack class and those classes derived from stack class. The members in class are private by default and those of struct/union are public by default. The example below explains the access specifiers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
struct testA {&lt;br /&gt;
  	friend class testC;&lt;br /&gt;
        private:&lt;br /&gt;
               int a;&lt;br /&gt;
        public:&lt;br /&gt;
               int b;&lt;br /&gt;
        protected:&lt;br /&gt;
               int c;&lt;br /&gt;
};&lt;br /&gt;
struct testB : testA {&lt;br /&gt;
  	void f() {&lt;br /&gt;
    	a = 1;  // Cannot be accessed since a is private in testA&lt;br /&gt;
       	b = 2;&lt;br /&gt;
    	c = 3;&lt;br /&gt;
  	}&lt;br /&gt;
};&lt;br /&gt;
struct testC {&lt;br /&gt;
  	void f(testA x) {&lt;br /&gt;
    		x.a = 4;&lt;br /&gt;
                x.b = 5;&lt;br /&gt;
    	        x.c = 6;&lt;br /&gt;
  		}&lt;br /&gt;
};&lt;br /&gt;
int main() {&lt;br /&gt;
  	testA y;&lt;br /&gt;
        y.a = 7;  // Cannot be accessed since a is private in testA&lt;br /&gt;
        y.b = 8;&lt;br /&gt;
        y.c = 9; // Cannot be accessed since c is protected in testA&lt;br /&gt;
&lt;br /&gt;
        testB z;&lt;br /&gt;
        z.a = 10; // Cannot be accessed since a is private in testA&lt;br /&gt;
        z.b = 11;&lt;br /&gt;
        z.c = 12; // Cannot be accessed since c is protected in testA&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
C++ also provides access specifiers for derived classes.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Base_Class&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    int Public_Func();    // Declare a public member.&lt;br /&gt;
protected:&lt;br /&gt;
    int Protected_Func(); // Declare a protected member.&lt;br /&gt;
private:&lt;br /&gt;
    int Private_Func();   // Declare a private member.&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Declare two classes derived from BaseClass.&lt;br /&gt;
class Derived_Class1 : public BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Derived_Class2 : private BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Derived_Class1, the member function Public_Func is a public member and Protected_Func is a protected member because Base_Class is a public base class. Private_Func is private to Base_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
In Derived_Class2, the functions Public_Func and Protected_Func are considered private members because Base_Class is a private base class. Again, Private_Func is private toBase_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] provides 4 types of access specifiers Public, Private, Protected and default.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Situation&lt;br /&gt;
! Public&lt;br /&gt;
! Protected&lt;br /&gt;
! default&lt;br /&gt;
! private&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from same Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from different Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no,unless it is&lt;br /&gt;
a subclass&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Public methods, variables and classes can be accessed from anywhere in the file system. The constraint that there should be just one public class per file is enforced.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square &lt;br /&gt;
{ 	 // public class&lt;br /&gt;
  public x, y;   // public instance variables&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Private variables and methods has only class level access. Not even subclasses can access those. Such kind off variables can be accessed by getter and setter methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square&lt;br /&gt;
 {   // public class&lt;br /&gt;
  	private double x, y;   // private (encapsulated) instance variables&lt;br /&gt;
&lt;br /&gt;
  	public setCorner(int x, int y) &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		this.x = x;&lt;br /&gt;
    		this.y = y;&lt;br /&gt;
  	}&lt;br /&gt;
&lt;br /&gt;
  	public getCorner() &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		return Point(x, y);&lt;br /&gt;
  	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Protected methods and variables can be accessed by the same class, its sub classes and those within the same package. While in Default the class, methods and fields have only package level access. Below illustrates an example on Protected and Private access specifiers.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class calculate&lt;br /&gt;
{&lt;br /&gt;
	protected double x,y;&lt;br /&gt;
	private double z;&lt;br /&gt;
&lt;br /&gt;
	calculate(double x, double y)&lt;br /&gt;
	{&lt;br /&gt;
		this.x = x;&lt;br /&gt;
		this.y = y;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class multiplication extends calculate&lt;br /&gt;
{&lt;br /&gt;
	void prod()&lt;br /&gt;
	{&lt;br /&gt;
		double result = x * y;&lt;br /&gt;
		// z = result;  // Since z is private&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby ===&lt;br /&gt;
The concept of private,protected and public methods in [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]is somewhere different than it is in languages like Java(the public concept is similar).&lt;br /&gt;
&lt;br /&gt;
Private&lt;br /&gt;
&lt;br /&gt;
In Ruby when a method is declared as private,it means it can never be called with an explicit receiver. This typically means that we can call a private method from within a class it is declared as well as subclasses of this class.Below illustrates an example for private access specifier.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  private&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
A.new.main_method&lt;br /&gt;
B.new.main_method&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, as soon as we try to use an explicit receiver, even if the receiver is &amp;quot;self&amp;quot;, the method call will fail .Below illustrates an example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
C.new.main_method&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
a.rb:36:in `main_method': private method `method1' called for #&amp;lt;C:0x7f67025a0648&amp;gt; (NoMethodError)&lt;br /&gt;
    from a.rb:40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus we see that a private method can never be called from outside the class hierarchy where it is defined.&lt;br /&gt;
&lt;br /&gt;
Protected methods can be called with an implicit receiver,just like a private method,but in addition to this it can be called with an explicit receiver as long as this receiver is self.Below illustrates an example of this:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  protected&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
hello from C&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see here unlike the private method ,call with implicit receiver in class B and explicit receiver in class C both succeed.&lt;br /&gt;
&lt;br /&gt;
Public methods are accessible with any kind of explicit or implicit receiver from anywhere.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In OO languages one of the key principle is encapsulation/data hiding. The examples of different OO programming languages have been illustrated above and how access control helps in achieving encapsulation. With the progress of each OO language the access specifiers definition have been formulated to suit the behaviour of the language.&lt;br /&gt;
&lt;br /&gt;
In java 1.0 release there was the private protected specifier. It had the same behavior of the protected modifier in C++, keeping access to subclasses, but not package members. It didn't fall into the nice progression of adding access, going from private to default to protected to public, as it was more like going from private to private protected to public - bypassing default all together and was hence dropped.&lt;br /&gt;
&lt;br /&gt;
Thus access specifiers are an important concept in OO languages and their importance and need should be understood by programmer's for better progrmming.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1]http://msdn.microsoft.com/en-us/library/7f45fat0(v=vs.80).aspx&lt;br /&gt;
&lt;br /&gt;
[2]http://staff.science.uva.nl/~heck/JAVAcourse/ch4/ss2_2.html&lt;br /&gt;
&lt;br /&gt;
[3]http://en.wikipedia.org/wiki/Class_(computer_programming)&lt;br /&gt;
&lt;br /&gt;
[4]http://www.skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/&lt;br /&gt;
&lt;br /&gt;
[5]http://www.jvoegele.com/software/langcomp.html&lt;br /&gt;
&lt;br /&gt;
[6]http://www.jot.fm/issues/issue_2005_05/article3/&lt;br /&gt;
&lt;br /&gt;
[7]http://www.jguru.com/faq/view.jsp?EID=15576&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=49996</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b jp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=49996"/>
		<updated>2011-09-21T15:51:15Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Access control is defined as the ability for a modules implementation to remain hidden behind its public interface. This is achieved in object oriented languages via encapsulation. Access control is closely related to the encapsulation/information hiding principle of object-oriented languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The term “object oriented programming” was first used by Xerox PARC in Smalltalk programming language so as to refer to the process of using objects as the foundation for computation. Since the introduction of OOP, a large number of modern programming languages are now using the concept. Some of these are [http://en.wikipedia.org/wiki/Fortran FORTRAN],[http://en.wikipedia.org/wiki/BASIC BASIC], and [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal]. There have been some compatibility issues, because many programs were not designed with a OOPs approach in mind.&lt;br /&gt;
To solve this researcher’s suggested designing languages that used OOP concepts and still retained many of the functions that programmers needed. Examples of this type are Eiffel, Java, and Ruby.&lt;br /&gt;
&lt;br /&gt;
== Need for Access Control In OO Languages ==&lt;br /&gt;
&lt;br /&gt;
One of the key OOP’s concept considered while designing programming languages is encapsulation. Encapsulation will allow a class to hide information from objects that may use the code. For example, the Cat class will have a purr() method. A code will be written which will explain how the cat purrs. Despite this, it is not necessary for Betsy the cat to know how she purrs. Encapsulation makes it easier for the developer to change the code without having to change the code for Betsy. When the structure of class is hidden, it is harder for it to be used in the wrong way. &lt;br /&gt;
&lt;br /&gt;
Encapsulation will basically state which class is allowed to use the members of a certain object. This concept makes it easier for programmers to deal with or avoid errors when developing a program. The members within OOP can be categorized as being protected, public, or private.&lt;br /&gt;
&lt;br /&gt;
== Access Control in various OO Languagues ==&lt;br /&gt;
&lt;br /&gt;
 	&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! &lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
|-&lt;br /&gt;
| Access &lt;br /&gt;
control&lt;br /&gt;
&lt;br /&gt;
|Public,protected,private,”friends&lt;br /&gt;
&lt;br /&gt;
|public,private,package,protected&lt;br /&gt;
&lt;br /&gt;
|private,public,protected&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
In [http://en.wikipedia.org/wiki/C%2B%2B C++] we have private, public and protected. Suppose variable “data” is a member of class stack and is private then it can accessed by members and friends of stack class. If the member is public then it has no access restrictions. But if the member is protected then it can be accessed by members and friends of stack class and those classes derived from stack class. The members in class are private by default and those of struct/union are public by default. The example below explains the access specifiers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
struct testA {&lt;br /&gt;
  	friend class testC;&lt;br /&gt;
        private:&lt;br /&gt;
               int a;&lt;br /&gt;
        public:&lt;br /&gt;
               int b;&lt;br /&gt;
        protected:&lt;br /&gt;
               int c;&lt;br /&gt;
};&lt;br /&gt;
struct testB : testA {&lt;br /&gt;
  	void f() {&lt;br /&gt;
    	a = 1;  // Cannot be accessed since a is private in testA&lt;br /&gt;
       	b = 2;&lt;br /&gt;
    	c = 3;&lt;br /&gt;
  	}&lt;br /&gt;
};&lt;br /&gt;
struct testC {&lt;br /&gt;
  	void f(testA x) {&lt;br /&gt;
    		x.a = 4;&lt;br /&gt;
                x.b = 5;&lt;br /&gt;
    	        x.c = 6;&lt;br /&gt;
  		}&lt;br /&gt;
};&lt;br /&gt;
int main() {&lt;br /&gt;
  	testA y;&lt;br /&gt;
        y.a = 7;  // Cannot be accessed since a is private in testA&lt;br /&gt;
        y.b = 8;&lt;br /&gt;
        y.c = 9; // Cannot be accessed since c is protected in testA&lt;br /&gt;
&lt;br /&gt;
        testB z;&lt;br /&gt;
        z.a = 10; // Cannot be accessed since a is private in testA&lt;br /&gt;
        z.b = 11;&lt;br /&gt;
        z.c = 12; // Cannot be accessed since c is protected in testA&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
C++ also provides access specifiers for derived classes.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Base_Class&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    int Public_Func();    // Declare a public member.&lt;br /&gt;
protected:&lt;br /&gt;
    int Protected_Func(); // Declare a protected member.&lt;br /&gt;
private:&lt;br /&gt;
    int Private_Func();   // Declare a private member.&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Declare two classes derived from BaseClass.&lt;br /&gt;
class Derived_Class1 : public BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Derived_Class2 : private BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Derived_Class1, the member function Public_Func is a public member and Protected_Func is a protected member because Base_Class is a public base class. Private_Func is private to Base_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
In Derived_Class2, the functions Public_Func and Protected_Func are considered private members because Base_Class is a private base class. Again, Private_Func is private toBase_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] provides 4 types of access specifiers Public, Private, Protected and default.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Situation&lt;br /&gt;
! Public&lt;br /&gt;
! Protected&lt;br /&gt;
! default&lt;br /&gt;
! private&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from same Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from different Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no,unless it is&lt;br /&gt;
a subclass&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Public methods, variables and classes can be accessed from anywhere in the file system. The constraint that there should be just one public class per file is enforced.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square &lt;br /&gt;
{ 	 // public class&lt;br /&gt;
  public x, y;   // public instance variables&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Private variables and methods has only class level access. Not even subclasses can access those. Such kind off variables can be accessed by getter and setter methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square&lt;br /&gt;
 {   // public class&lt;br /&gt;
  	private double x, y;   // private (encapsulated) instance variables&lt;br /&gt;
&lt;br /&gt;
  	public setCorner(int x, int y) &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		this.x = x;&lt;br /&gt;
    		this.y = y;&lt;br /&gt;
  	}&lt;br /&gt;
&lt;br /&gt;
  	public getCorner() &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		return Point(x, y);&lt;br /&gt;
  	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Protected methods and variables can be accessed by the same class, its sub classes and those within the same package. While in Default the class, methods and fields have only package level access. Below illustrates an example on Protected and Private access specifiers.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class calculate&lt;br /&gt;
{&lt;br /&gt;
	protected double x,y;&lt;br /&gt;
	private double z;&lt;br /&gt;
&lt;br /&gt;
	calculate(double x, double y)&lt;br /&gt;
	{&lt;br /&gt;
		this.x = x;&lt;br /&gt;
		this.y = y;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class multiplication extends calculate&lt;br /&gt;
{&lt;br /&gt;
	void prod()&lt;br /&gt;
	{&lt;br /&gt;
		double result = x * y;&lt;br /&gt;
		// z = result;  // Since z is private&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby ===&lt;br /&gt;
The concept of private,protected and public methods in [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]is somewhere different than it is in languages like Java(the public concept is similar).&lt;br /&gt;
&lt;br /&gt;
Private&lt;br /&gt;
&lt;br /&gt;
In Ruby when a method is declared as private,it means it can never be called with an explicit receiver. This typically means that we can call a private method from within a class it is declared as well as subclasses of this class.Below illustrates an example for private access specifier.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  private&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
A.new.main_method&lt;br /&gt;
B.new.main_method&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, as soon as we try to use an explicit receiver, even if the receiver is &amp;quot;self&amp;quot;, the method call will fail .Below illustrates an example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
C.new.main_method&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
a.rb:36:in `main_method': private method `method1' called for #&amp;lt;C:0x7f67025a0648&amp;gt; (NoMethodError)&lt;br /&gt;
    from a.rb:40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus we see that a private method can never be called from outside the class hierarchy where it is defined.&lt;br /&gt;
&lt;br /&gt;
Protected methods can be called with an implicit receiver,just like a private method,but in addition to this it can be called with an explicit receiver as long as this receiver is self.Below illustrates an example of this:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  protected&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
hello from C&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see here unlike the private method ,call with implicit receiver in class B and explicit receiver in class C both succeed.&lt;br /&gt;
&lt;br /&gt;
Public methods are accessible with any kind of explicit or implicit receiver from anywhere.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
Conclusion&lt;br /&gt;
&lt;br /&gt;
In OO languages one of the key principle is encapsulation/data hiding. The examples of different OO programming languages have been illustrated above and how access control helps in achieving encapsulation. With the progress of each OO language the access specifiers definition have been formulated to suit the behaviour of the language.&lt;br /&gt;
&lt;br /&gt;
In java 1.0 release there was the private protected specifier. It had the same behavior of the protected modifier in C++, keeping access to subclasses, but not package members. It didn't fall into the nice progression of adding access, going from private to default to protected to public, as it was more like going from private to private protected to public - bypassing default all together and was hence dropped.&lt;br /&gt;
&lt;br /&gt;
Thus access specifiers are an important concept in OO languages and their importance and need should be understood by programmer's for better progrmming.&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1]http://msdn.microsoft.com/en-us/library/7f45fat0(v=vs.80).aspx&lt;br /&gt;
&lt;br /&gt;
[2]http://staff.science.uva.nl/~heck/JAVAcourse/ch4/ss2_2.html&lt;br /&gt;
&lt;br /&gt;
[3]http://en.wikipedia.org/wiki/Class_(computer_programming)&lt;br /&gt;
&lt;br /&gt;
[4]http://www.skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/&lt;br /&gt;
&lt;br /&gt;
[5]http://www.jvoegele.com/software/langcomp.html&lt;br /&gt;
&lt;br /&gt;
[6]http://www.jot.fm/issues/issue_2005_05/article3/&lt;br /&gt;
&lt;br /&gt;
[7]http://www.jguru.com/faq/view.jsp?EID=15576&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=49995</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b jp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_jp&amp;diff=49995"/>
		<updated>2011-09-21T15:50:48Z</updated>

		<summary type="html">&lt;p&gt;Ppreeti: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Access control is defined as the ability for a modules implementation to remain hidden behind its public interface. This is achieved in object oriented languages via encapsulation. Access control is closely related to the encapsulation/information hiding principle of object-oriented languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The term “object oriented programming” was first used by Xerox PARC in Smalltalk programming language so as to refer to the process of using objects as the foundation for computation. Since the introduction of OOP, a large number of modern programming languages are now using the concept. Some of these are [http://en.wikipedia.org/wiki/Fortran FORTRAN],[http://en.wikipedia.org/wiki/BASIC BASIC], and [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal]. There have been some compatibility issues, because many programs were not designed with a OOPs approach in mind.&lt;br /&gt;
To solve this researcher’s suggested designing languages that used OOP concepts and still retained many of the functions that programmers needed. Examples of this type are Eiffel, Java, and Ruby.&lt;br /&gt;
&lt;br /&gt;
== Need for Access Control In OO Languages ==&lt;br /&gt;
&lt;br /&gt;
One of the key OOP’s concept considered while designing programming languages is encapsulation. Encapsulation will allow a class to hide information from objects that may use the code. For example, the Cat class will have a purr() method. A code will be written which will explain how the cat purrs. Despite this, it is not necessary for Betsy the cat to know how she purrs. Encapsulation makes it easier for the developer to change the code without having to change the code for Betsy. When the structure of class is hidden, it is harder for it to be used in the wrong way. &lt;br /&gt;
&lt;br /&gt;
Encapsulation will basically state which class is allowed to use the members of a certain object. This concept makes it easier for programmers to deal with or avoid errors when developing a program. The members within OOP can be categorized as being protected, public, or private.&lt;br /&gt;
&lt;br /&gt;
== Access Control in various OO Languagues ==&lt;br /&gt;
&lt;br /&gt;
 	&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! &lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
|-&lt;br /&gt;
| Access &lt;br /&gt;
control&lt;br /&gt;
&lt;br /&gt;
|Public,protected,private,”friends&lt;br /&gt;
&lt;br /&gt;
|public,private,package,protected&lt;br /&gt;
&lt;br /&gt;
|private,public,protected&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
In [http://en.wikipedia.org/wiki/C%2B%2B C++] we have private, public and protected. Suppose variable “data” is a member of class stack and is private then it can accessed by members and friends of stack class. If the member is public then it has no access restrictions. But if the member is protected then it can be accessed by members and friends of stack class and those classes derived from stack class. The members in class are private by default and those of struct/union are public by default. The example below explains the access specifiers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
struct testA {&lt;br /&gt;
  	friend class testC;&lt;br /&gt;
        private:&lt;br /&gt;
               int a;&lt;br /&gt;
        public:&lt;br /&gt;
               int b;&lt;br /&gt;
        protected:&lt;br /&gt;
               int c;&lt;br /&gt;
};&lt;br /&gt;
struct testB : testA {&lt;br /&gt;
  	void f() {&lt;br /&gt;
    	a = 1;  // Cannot be accessed since a is private in testA&lt;br /&gt;
       	b = 2;&lt;br /&gt;
    	c = 3;&lt;br /&gt;
  	}&lt;br /&gt;
};&lt;br /&gt;
struct testC {&lt;br /&gt;
  	void f(testA x) {&lt;br /&gt;
    		x.a = 4;&lt;br /&gt;
                x.b = 5;&lt;br /&gt;
    	        x.c = 6;&lt;br /&gt;
  		}&lt;br /&gt;
};&lt;br /&gt;
int main() {&lt;br /&gt;
  	testA y;&lt;br /&gt;
        y.a = 7;  // Cannot be accessed since a is private in testA&lt;br /&gt;
        y.b = 8;&lt;br /&gt;
        y.c = 9; // Cannot be accessed since c is protected in testA&lt;br /&gt;
&lt;br /&gt;
        testB z;&lt;br /&gt;
        z.a = 10; // Cannot be accessed since a is private in testA&lt;br /&gt;
        z.b = 11;&lt;br /&gt;
        z.c = 12; // Cannot be accessed since c is protected in testA&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
C++ also provides access specifiers for derived classes.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Base_Class&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    int Public_Func();    // Declare a public member.&lt;br /&gt;
protected:&lt;br /&gt;
    int Protected_Func(); // Declare a protected member.&lt;br /&gt;
private:&lt;br /&gt;
    int Private_Func();   // Declare a private member.&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Declare two classes derived from BaseClass.&lt;br /&gt;
class Derived_Class1 : public BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Derived_Class2 : private BaseClass&lt;br /&gt;
{&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Derived_Class1, the member function Public_Func is a public member and Protected_Func is a protected member because Base_Class is a public base class. Private_Func is private to Base_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
In Derived_Class2, the functions Public_Func and Protected_Func are considered private members because Base_Class is a private base class. Again, Private_Func is private toBase_Class, and it is inaccessible to any derived classes.&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] provides 4 types of access specifiers Public, Private, Protected and default.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Situation&lt;br /&gt;
! Public&lt;br /&gt;
! Protected&lt;br /&gt;
! default&lt;br /&gt;
! private&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from same Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Accessible to class&lt;br /&gt;
from different Package?&lt;br /&gt;
&lt;br /&gt;
|yes&lt;br /&gt;
&lt;br /&gt;
|no,unless it is&lt;br /&gt;
a subclass&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|no&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Public methods, variables and classes can be accessed from anywhere in the file system. The constraint that there should be just one public class per file is enforced.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square &lt;br /&gt;
{ 	 // public class&lt;br /&gt;
  public x, y;   // public instance variables&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Private variables and methods has only class level access. Not even subclasses can access those. Such kind off variables can be accessed by getter and setter methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Square&lt;br /&gt;
 {   // public class&lt;br /&gt;
  	private double x, y;   // private (encapsulated) instance variables&lt;br /&gt;
&lt;br /&gt;
  	public setCorner(int x, int y) &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		this.x = x;&lt;br /&gt;
    		this.y = y;&lt;br /&gt;
  	}&lt;br /&gt;
&lt;br /&gt;
  	public getCorner() &lt;br /&gt;
{  // setting values of private fields&lt;br /&gt;
    		return Point(x, y);&lt;br /&gt;
  	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Protected methods and variables can be accessed by the same class, its sub classes and those within the same package. While in Default the class, methods and fields have only package level access. Below illustrates an example on Protected and Private access specifiers.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class calculate&lt;br /&gt;
{&lt;br /&gt;
	protected double x,y;&lt;br /&gt;
	private double z;&lt;br /&gt;
&lt;br /&gt;
	calculate(double x, double y)&lt;br /&gt;
	{&lt;br /&gt;
		this.x = x;&lt;br /&gt;
		this.y = y;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class multiplication extends calculate&lt;br /&gt;
{&lt;br /&gt;
	void prod()&lt;br /&gt;
	{&lt;br /&gt;
		double result = x * y;&lt;br /&gt;
		// z = result;  // Since z is private&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby ===&lt;br /&gt;
The concept of private,protected and public methods in [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]is somewhere different than it is in languages like Java(the public concept is similar).&lt;br /&gt;
&lt;br /&gt;
Private&lt;br /&gt;
&lt;br /&gt;
In Ruby when a method is declared as private,it means it can never be called with an explicit receiver. This typically means that we can call a private method from within a class it is declared as well as subclasses of this class.Below illustrates an example for private access specifier.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  private&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
A.new.main_method&lt;br /&gt;
B.new.main_method&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, as soon as we try to use an explicit receiver, even if the receiver is &amp;quot;self&amp;quot;, the method call will fail .Below illustrates an example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
C.new.main_method&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
a.rb:36:in `main_method': private method `method1' called for #&amp;lt;C:0x7f67025a0648&amp;gt; (NoMethodError)&lt;br /&gt;
    from a.rb:40&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Thus we see that a private method can never be called from outside the class hierarchy where it is defined.&lt;br /&gt;
&lt;br /&gt;
Protected methods can be called with an implicit receiver,just like a private method,but in addition to this it can be called with an explicit receiver as long as this receiver is self.Below illustrates an example of this:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  protected&lt;br /&gt;
  def method1&lt;br /&gt;
    puts &amp;quot;hello from #{self.class}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class C &amp;lt; A&lt;br /&gt;
  def main_method&lt;br /&gt;
    self.method1&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;br /&gt;
alan@alan-ubuntu-vm:~/tmp$ ruby a.rb&lt;br /&gt;
hello from A&lt;br /&gt;
hello from B&lt;br /&gt;
hello from C&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus we see here unlike the private method ,call with implicit receiver in class B and explicit receiver in class C both succeed.&lt;br /&gt;
&lt;br /&gt;
Public methods are accessible with any kind of explicit or implicit receiver from anywhere.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
Conclusion&lt;br /&gt;
&lt;br /&gt;
In OO languages one of the key principle is encapsulation/data hiding. The examples of different OO programming languages have been illustrated above and how access control helps in achieving encapsulation. With the progress of each OO language the access specifiers definition have been formulated to suit the behaviour of the language.&lt;br /&gt;
&lt;br /&gt;
In java 1.0 release there was the private protected specifier. It had the same behavior of the protected modifier in C++, keeping access to subclasses, but not package members. It didn't fall into the nice progression of adding access, going from private to default to protected to public, as it was more like going from private to private protected to public - bypassing default all together and was hence dropped.&lt;br /&gt;
&lt;br /&gt;
Thus access specifiers are an important concept in OO languages and their importance and need should be understood by programmer's for better progrmming.&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1]http://msdn.microsoft.com/en-us/library/7f45fat0(v=vs.80).aspx&lt;br /&gt;
&lt;br /&gt;
[2]http://staff.science.uva.nl/~heck/JAVAcourse/ch4/ss2_2.html&lt;br /&gt;
&lt;br /&gt;
[3]http://en.wikipedia.org/wiki/Class_(computer_programming)&lt;br /&gt;
&lt;br /&gt;
[4]http://www.skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/&lt;br /&gt;
&lt;br /&gt;
[5]http://www.jvoegele.com/software/langcomp.html&lt;br /&gt;
&lt;br /&gt;
[6]http://www.jot.fm/issues/issue_2005_05/article3/&lt;/div&gt;</summary>
		<author><name>Ppreeti</name></author>
	</entry>
</feed>