<?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=Psivash2</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=Psivash2"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Psivash2"/>
	<updated>2026-06-08T03:07:35Z</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_2012/ch2b_2w45_pg&amp;diff=70590</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w45 pg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70590"/>
		<updated>2012-11-19T15:57:48Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Command Pattern =&lt;br /&gt;
&lt;br /&gt;
= Adapter Pattern, a directory of sites =&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Command Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Command Pattern.&lt;br /&gt;
&lt;br /&gt;
= &amp;lt;b&amp;gt;Introduction&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;[http://en.wikipedia.org/wiki/Command_pattern Definition:]&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command pattern is a behavioural design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
Three terms always associated with the command pattern are client, invoker and receiver. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Client&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The client instantiates the command object and provides the information required to call the method at a later time. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Invoker&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The invoker decides when the method should be called. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Receiver&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;How does it works?&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.&lt;br /&gt;
 &lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
[http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html Command Real world Example ]&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parameterized with different requests. The &amp;quot;check&amp;quot; at a diner is an example of a Command pattern. The waiter or waitress takes an order, or command from a customer, and encapsulates that order by writing it on the check. The order is then queued for a short order cook. Note that the pad of &amp;quot;checks&amp;quot; used by different diners is not dependent on the menu, and therefore they can support commands to cook many different items.&lt;br /&gt;
&lt;br /&gt;
[[File:http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;When Would I Use This Pattern?&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The Command Pattern is useful when:&amp;lt;br&amp;gt;&lt;br /&gt;
•	A history of requests is needed&amp;lt;br&amp;gt;&lt;br /&gt;
•	You need callback functionality&amp;lt;br&amp;gt;&lt;br /&gt;
•	Requests need to be handled at variant times or in variant orders&amp;lt;br&amp;gt;&lt;br /&gt;
•	The invoker should be decoupled from the object handling the invocation.&amp;lt;br&amp;gt;&lt;br /&gt;
You'll see command being used a lot when you need to have multiple undo operations, where a stack of the recently executed commands are maintained. To implement the undo, all you need to do is get the last Command in the stack and execute it's undo() method.&lt;br /&gt;
You'll also find Command useful for wizards, progress bars, GUI buttons and menu actions, and other transactional behavior. &lt;br /&gt;
&lt;br /&gt;
= &amp;lt;b&amp;gt;Directory of sites&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; = &lt;br /&gt;
== &amp;lt;b&amp;gt;[http://msdn.microsoft.com/en-us/magazine/cc163920.aspx Command pattern on MSDN Microsoft Website] &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; ==&lt;br /&gt;
Command Pattern to -Simplify Distributed System Design Using the Command Pattern, MSMQ, and .NET &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Object-based collaboration VS service-based collaboration&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	Communication or exchange of request and service and the format or the pattern based on play an important criteria in successful implementation of  object-based collaboration and service-oriented architecture. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
•	Systems generally share a common platform, allowing you to optimize in ways you can't on widely dispersed -systems that span various operating systems and development environments are preferred to follow object-based collaboration than service-based collaboration.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
•	But generally the participants are hybrid and they don't share a common platform,  in such situations service oriented architecture comes handy. Here the requests are queued in Microsoft Message Queuing (MSMQ) which implements Command design pattern. &lt;br /&gt;
&lt;br /&gt;
[[File:http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif]]&lt;br /&gt;
&lt;br /&gt;
The distributed command system discussed here is the combination of reliable messaging, services, and the Command pattern. While most often seen as a design pattern for implementing desktop application menu systems, the Command pattern offers advantages for distributed systems when combined with a robust messaging infrastructure like MSMQ. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Learning: &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The concept is very simple, in service oriented architecture, the participants don't share a common platform, hence you need a mechanism, a design pattern, a module to issue requests to objects.  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The module that is doing the requesting doesn’t know what the receiver will be, or what operation will be requested.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pattern and Participants&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class	              Description&lt;br /&gt;
Command	              Abstract base class for all Command objects&lt;br /&gt;
Concrete command      Any number of classes derived from Command&lt;br /&gt;
Client	              The creator of a command&lt;br /&gt;
Invoker	              Executes a command&lt;br /&gt;
Receiver	      Any class that is acted on by a command&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Another Example: &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In ASP.NET, the database communication are based on the commands, the database commands such as create connection, command statement, parameter doesn't know what the object is and what the object it is requesting and these are typical example of command design pattern it best.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;&lt;br /&gt;
Conclusion&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Command pattern specifies a set of interactions that you can apply to the development of distributed systems in .NET. Use this pattern in your application to implement a high-performance, extendable backbone connecting your near link nodes that run .NET-enabled operating systems to achieve optimal performance and rapid development. Distributed commands are a mix of classic object orientation, messaging, and service architecture that together fill a definite need in the distributed systems space.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;[http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture Advantages]&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The benefit of the Command class is that it makes it easier to add new functions to an application. &lt;br /&gt;
If we add a new capability, we no longer need to reopen the Controller class to code routing information. &lt;br /&gt;
Instead, we encapsulate the information in a Command object and assign it to whatever control we use in the View to invoke the new capability. &lt;br /&gt;
When the Command gets passed to the Controller, the Controller can use it to route the request, without knowing anything about it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Disadvantages:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
== [http://www.phpwact.org/ Command Pattern on Web Application Component Toolkit (WACT)]&amp;lt;br&amp;gt; == &lt;br /&gt;
&lt;br /&gt;
[http://www.phpwact.org/pattern/command Web Application Component Toolkit]&lt;br /&gt;
The Web Application Component Toolkit is a framework for creating web applications. WACT facilitates a modular approach where individual, independent or reusable components may be integrated into a larger web application. WACT assists in implementing the Model View Controller pattern and the related Domain Model, Template View, Front Controller and Application Controller patterns. WACT emphasizes writing secure web applications.  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;WACT sees Command Pattern as&amp;lt;/b&amp;gt;&lt;br /&gt;
A GoF Design Pattern with the intent: “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”&lt;br /&gt;
&lt;br /&gt;
In WACT, this is seen with the FormController, where users can define a class which must contain the method performAction();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class AddressForm extends PostFormController {&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
 &lt;br /&gt;
    function InitializeActions() {&lt;br /&gt;
        $this-&amp;gt;registerSubmitAction(&amp;quot;register&amp;quot;, &amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
        $this-&amp;gt;registerDefaultSubmitAction(&amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The registerSubmitAction call above points the controller at the class RegisterUser, which is where the Command pattern resides.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class RegisterUser {&lt;br /&gt;
 &lt;br /&gt;
    function performAction(&amp;amp;$context) {&lt;br /&gt;
        $DataSpace =&amp;amp; $context-&amp;gt;getDataSpace();&lt;br /&gt;
        $Page =&amp;amp; new Template('/input/thanks.html');&lt;br /&gt;
        $Page-&amp;gt;import($DataSpace-&amp;gt;export());&lt;br /&gt;
        $Page-&amp;gt;display();&lt;br /&gt;
        return FORM_COMPLETE;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The FormController expects to find the performAction method in the Action Class RegisterUser, calling it when the form is submitted (and successfully validated).&lt;br /&gt;
&lt;br /&gt;
The Command pattern is a Gang of Four Design Pattern, used to allow a function name to be wrapped up in a class namespace, so that some code, which will call the function, can be given an instance of one of many classes without caring which which instance it’s dealing with.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Learning:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Here the Form controller in its post functionality exhibits command pattern as it requires the object that calls the post form functionality to implement performAction method, the actions that the object needs to perform when the form is submitted, but the post form controller doesn't know what are actions performed by the performAction method and whose performAction  method is called all it requires is it needs a class that has the performAction method to post it forms. &lt;br /&gt;
Hence the performAction acts like a command that is being called whenever the form is posted.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Another Example:&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PHP example &lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
/* interface */ class Tag {&lt;br /&gt;
   // The abstract command method&lt;br /&gt;
   function render($content);&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Bold {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;b&amp;gt;$content&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Italic {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;i&amp;gt;$content&amp;lt;/i&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
$commands = array (&lt;br /&gt;
    new Bold(),new Italic(), new Italic(), new Bold()&lt;br /&gt;
);&lt;br /&gt;
 &lt;br /&gt;
// Iterate over the command objects&lt;br /&gt;
foreach ( $commands as $command ) {&lt;br /&gt;
    echo ( $command-&amp;gt;render('Hello World!') );&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the PHP example provided above, class Bold, Italic works on the function render.&lt;br /&gt;
&lt;br /&gt;
In class Tag, all it does is for each command in the command object, render functionality is called, the render doesn't know which command, bold or italics is called and what doesn't knows the functionality of command (bold or italics) &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Applications&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Command pattern is used widely on in web based application frameworks like Java Struts from a PHP Perspective, usually in Action Classes, to allow framework users to define logic which responds to “events” in the application. Action Classes can be regarded as the “verbs” of the framework - they act on things - they do things.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Comparison to similar patterns&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Commands and Mementos have some similarity due to the fact they both work with an object's internal properties. The Command pattern keeps a record of changes to an object's state, and applies those changes in an ad-hoc fashion. A Memento also records changes to an object's state, and can restore that state at any time. The Chain of Responsibility pattern seems to handle processing in a similar manner to the Command, except it hands off processing to another process linearly unlike the Command pattern. An Interpreter pattern works in the example above because we are using language elements to determine which changes to apply at a given time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Advantages:&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	Since the object that implements the command design doesn't know what function it does and what object it affects, as it acts like a black box, the implementation of the objects that is calling the black box can be changed and it can be implemented independently without affecting the flow of the program.&lt;br /&gt;
&lt;br /&gt;
•	Hence command pattern gives a wide range of flexibility and sophisticated design features.&lt;br /&gt;
&amp;lt;b&amp;gt;Disadvantages:&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	It complicates the application design. Every pattern complicates application design; they’re used because the benefits outweigh the cost. In fact, they should only be used when the benefits outweigh the costs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== [http://www.javaworld.com/javatips/jw-javatip68.html?page=1 Command Pattern on Javaworld] == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;According to Javaworld,&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The design patterns not only accelerate the design phase of an object-oriented (OO) project but also increase the productivity of the development team and quality of the software. &lt;br /&gt;
&lt;br /&gt;
They describe a Command pattern as an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Sender     -  invokes an operation&lt;br /&gt;
Receiver   - receives the request to execute a certain operation. &lt;br /&gt;
Decoupling - the sender has no knowledge of the Receiver's interface&lt;br /&gt;
Request    - the command that is to be executed. &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Command pattern provides us flexibility as well as extensibility.&lt;br /&gt;
&lt;br /&gt;
The program discussed in the javaworld web directory is &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Fan {&lt;br /&gt;
        public void startRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void stopRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is not rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Light {&lt;br /&gt;
        public void turnOn( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is on &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void turnOff( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is off&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Switch {&lt;br /&gt;
        private Command UpCommand, DownCommand;&lt;br /&gt;
        public Switch( Command Up, Command Down) {&lt;br /&gt;
                UpCommand = Up; // concrete Command registers itself with the invoker &lt;br /&gt;
                DownCommand = Down;&lt;br /&gt;
        }&lt;br /&gt;
        void flipUp( ) { // invoker calls back concrete Command, which executes the Command on the receiver &lt;br /&gt;
                        UpCommand . execute ( ) ;                           &lt;br /&gt;
        }&lt;br /&gt;
        void flipDown( ) {&lt;br /&gt;
                        DownCommand . execute ( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOnCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOnCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOn( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOffCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOffCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOff( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOnCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOnCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . startRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOffCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOffCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . stopRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
public class TestCommand {&lt;br /&gt;
                public static void main(String[] args) {&lt;br /&gt;
                        Light  testLight = new Light( );&lt;br /&gt;
                        LightOnCommand testLOC = new LightOnCommand(testLight);&lt;br /&gt;
                        LightOffCommand testLFC = new LightOffCommand(testLight);&lt;br /&gt;
                        Switch testSwitch = new Switch( testLOC,testLFC);       &lt;br /&gt;
                        testSwitch.flipUp( );&lt;br /&gt;
                        testSwitch.flipDown( );&lt;br /&gt;
                        Fan testFan = new Fan( );&lt;br /&gt;
                        FanOnCommand foc = new FanOnCommand(testFan);&lt;br /&gt;
                        FanOffCommand ffc = new FanOffCommand(testFan);&lt;br /&gt;
                        Switch ts = new Switch( foc,ffc);&lt;br /&gt;
                        ts.flipUp( );&lt;br /&gt;
                        ts.flipDown( ); &lt;br /&gt;
                }&lt;br /&gt;
}               &lt;br /&gt;
Command.java&lt;br /&gt;
public interface Command {&lt;br /&gt;
        public abstract void execute ( );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Learning:&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	In the above example,&lt;br /&gt;
The class Fan has startRotate() and stopRotate() methods,the class Light has turnOn() and turnOff() methods.&lt;br /&gt;
The catch here is startRotate of Fan and turnOn of Light is performed when the switch is turned on and stopRotate of Fan and turnOff of Light is performed when the switch in turned off.&lt;br /&gt;
&lt;br /&gt;
•	Keeping this real world catch, the class switch is created as a kind of interface for &lt;br /&gt;
fan and light to work on it turn on and turn off requests.&lt;br /&gt;
&lt;br /&gt;
•	The switch has flipUp and flipDown methods which acts like command design implementation.&lt;br /&gt;
The switch doesn't know what action it performs on flipUp and flipDown and what object is affected when flipUp and flipDown is pressed. &lt;br /&gt;
&lt;br /&gt;
•	Hence flipUp and flipDown acts like a command without knowing what function it does and what object it affects, but eventually performing the required action. That is the beauty of command design. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;The Command pattern has the following advantages:&amp;lt;/b&amp;gt;&lt;br /&gt;
1.	A command decouples the object that invokes the operation from the one that knows how to perform it.&lt;br /&gt;
2.	A command is a first-class object. It can be manipulated and extended like any other object.&lt;br /&gt;
3.	Commands can be assembled into a composite command.&lt;br /&gt;
4.	It's easy to add new commands, because you don't have to change the existing classes.&lt;br /&gt;
&lt;br /&gt;
= &amp;lt;b&amp;gt;References&amp;lt;/b&amp;gt; = &lt;br /&gt;
&amp;lt;br&amp;gt;http://www.itsmedia.ca/the-command-pattern-and-restful-interfaces/&lt;br /&gt;
&amp;lt;br&amp;gt;http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
&amp;lt;br&amp;gt;http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
&amp;lt;br&amp;gt;http://www.phpwact.org/&lt;br /&gt;
&amp;lt;br&amp;gt;http://www.phpwact.org/pattern/command&lt;br /&gt;
&amp;lt;br&amp;gt;http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
&amp;lt;br&amp;gt;http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
&amp;lt;br&amp;gt;http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
&amp;lt;br&amp;gt;http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html&lt;br /&gt;
&amp;lt;br&amp;gt;http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70585</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w45 pg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70585"/>
		<updated>2012-11-19T15:30:34Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Command Pattern =&lt;br /&gt;
&lt;br /&gt;
= Adapter Pattern, a directory of sites =&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Command Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Command Pattern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Introduction&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;[http://en.wikipedia.org/wiki/Command_pattern Definition:]&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command pattern is a behavioural design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
Three terms always associated with the command pattern are client, invoker and receiver. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Client&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The client instantiates the command object and provides the information required to call the method at a later time. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Invoker&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The invoker decides when the method should be called. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Receiver&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;How does it works?&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.&lt;br /&gt;
 &lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
[http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html Command Real world Example ]&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parameterized with different requests. The &amp;quot;check&amp;quot; at a diner is an example of a Command pattern. The waiter or waitress takes an order, or command from a customer, and encapsulates that order by writing it on the check. The order is then queued for a short order cook. Note that the pad of &amp;quot;checks&amp;quot; used by different diners is not dependent on the menu, and therefore they can support commands to cook many different items.&lt;br /&gt;
&lt;br /&gt;
[[File:http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;When Would I Use This Pattern?&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The Command Pattern is useful when:&amp;lt;br&amp;gt;&lt;br /&gt;
•	A history of requests is needed&amp;lt;br&amp;gt;&lt;br /&gt;
•	You need callback functionality&amp;lt;br&amp;gt;&lt;br /&gt;
•	Requests need to be handled at variant times or in variant orders&amp;lt;br&amp;gt;&lt;br /&gt;
•	The invoker should be decoupled from the object handling the invocation.&amp;lt;br&amp;gt;&lt;br /&gt;
You'll see command being used a lot when you need to have multiple undo operations, where a stack of the recently executed commands are maintained. To implement the undo, all you need to do is get the last Command in the stack and execute it's undo() method.&lt;br /&gt;
You'll also find Command useful for wizards, progress bars, GUI buttons and menu actions, and other transactional behavior. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Directory of sites&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;[http://msdn.microsoft.com/en-us/magazine/cc163920.aspx Command pattern on MSDN Microsoft Website] &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Command Pattern to -Simplify Distributed System Design Using the Command Pattern, MSMQ, and .NET &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Object-based collaboration VS service-based collaboration&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	Communication or exchange of request and service and the format or the pattern based on play an important criteria in successful implementation of  object-based collaboration and service-oriented architecture. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
•	Systems generally share a common platform, allowing you to optimize in ways you can't on widely dispersed -systems that span various operating systems and development environments are preferred to follow object-based collaboration than service-based collaboration.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
•	But generally the participants are hybrid and they don't share a common platform,  in such situations service oriented architecture comes handy. Here the requests are queued in Microsoft Message Queuing (MSMQ) which implements Command design pattern. &lt;br /&gt;
&lt;br /&gt;
[[File:http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif]]&lt;br /&gt;
&lt;br /&gt;
The distributed command system discussed here is the combination of reliable messaging, services, and the Command pattern. While most often seen as a design pattern for implementing desktop application menu systems, the Command pattern offers advantages for distributed systems when combined with a robust messaging infrastructure like MSMQ. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Learning: &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The concept is very simple, in service oriented architecture, the participants don't share a common platform, hence you need a mechanism, a design pattern, a module to issue requests to objects.  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The module that is doing the requesting doesn’t know what the receiver will be, or what operation will be requested.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pattern and Participants&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class	              Description&lt;br /&gt;
Command	              Abstract base class for all Command objects&lt;br /&gt;
Concrete command      Any number of classes derived from Command&lt;br /&gt;
Client	              The creator of a command&lt;br /&gt;
Invoker	              Executes a command&lt;br /&gt;
Receiver	      Any class that is acted on by a command&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Another Example: &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In ASP.NET, the database communication are based on the commands, the database commands such as create connection, command statement, parameter doesn't know what the object is and what the object it is requesting and these are typical example of command design pattern it best.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;&lt;br /&gt;
Conclusion&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Command pattern specifies a set of interactions that you can apply to the development of distributed systems in .NET. Use this pattern in your application to implement a high-performance, extendable backbone connecting your near link nodes that run .NET-enabled operating systems to achieve optimal performance and rapid development. Distributed commands are a mix of classic object orientation, messaging, and service architecture that together fill a definite need in the distributed systems space.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&lt;br /&gt;
[http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture Advantages]&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefit of the Command class is that it makes it easier to add new functions to an application. &lt;br /&gt;
If we add a new capability, we no longer need to reopen the Controller class to code routing information. &lt;br /&gt;
Instead, we encapsulate the information in a Command object and assign it to whatever control we use in the View to invoke the new capability. &lt;br /&gt;
When the Command gets passed to the Controller, the Controller can use it to route the request, without knowing anything about it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Disadvantages:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
[http://www.phpwact.org/ Command Pattern on Web Application Component Toolkit (WACT)]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.phpwact.org/pattern/command Web Application Component Toolkit]&lt;br /&gt;
The Web Application Component Toolkit is a framework for creating web applications. WACT facilitates a modular approach where individual, independent or reusable components may be integrated into a larger web application. WACT assists in implementing the Model View Controller pattern and the related Domain Model, Template View, Front Controller and Application Controller patterns. WACT emphasizes writing secure web applications.  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;WACT sees Command Pattern as&amp;lt;/b&amp;gt;&lt;br /&gt;
A GoF Design Pattern with the intent: “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”&lt;br /&gt;
&lt;br /&gt;
In WACT, this is seen with the FormController, where users can define a class which must contain the method performAction();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class AddressForm extends PostFormController {&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
 &lt;br /&gt;
    function InitializeActions() {&lt;br /&gt;
        $this-&amp;gt;registerSubmitAction(&amp;quot;register&amp;quot;, &amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
        $this-&amp;gt;registerDefaultSubmitAction(&amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The registerSubmitAction call above points the controller at the class RegisterUser, which is where the Command pattern resides.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class RegisterUser {&lt;br /&gt;
 &lt;br /&gt;
    function performAction(&amp;amp;$context) {&lt;br /&gt;
        $DataSpace =&amp;amp; $context-&amp;gt;getDataSpace();&lt;br /&gt;
        $Page =&amp;amp; new Template('/input/thanks.html');&lt;br /&gt;
        $Page-&amp;gt;import($DataSpace-&amp;gt;export());&lt;br /&gt;
        $Page-&amp;gt;display();&lt;br /&gt;
        return FORM_COMPLETE;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The FormController expects to find the performAction method in the Action Class RegisterUser, calling it when the form is submitted (and successfully validated).&lt;br /&gt;
&lt;br /&gt;
The Command pattern is a Gang of Four Design Pattern, used to allow a function name to be wrapped up in a class namespace, so that some code, which will call the function, can be given an instance of one of many classes without caring which which instance it’s dealing with.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Learning:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Here the Form controller in its post functionality exhibits command pattern as it requires the object that calls the post form functionality to implement performAction method, the actions that the object needs to perform when the form is submitted, but the post form controller doesn't know what are actions performed by the performAction method and whose performAction  method is called all it requires is it needs a class that has the performAction method to post it forms. &lt;br /&gt;
Hence the performAction acts like a command that is being called whenever the form is posted.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Another Example:&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PHP example &lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
/* interface */ class Tag {&lt;br /&gt;
   // The abstract command method&lt;br /&gt;
   function render($content);&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Bold {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;b&amp;gt;$content&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Italic {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;i&amp;gt;$content&amp;lt;/i&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
$commands = array (&lt;br /&gt;
    new Bold(),new Italic(), new Italic(), new Bold()&lt;br /&gt;
);&lt;br /&gt;
 &lt;br /&gt;
// Iterate over the command objects&lt;br /&gt;
foreach ( $commands as $command ) {&lt;br /&gt;
    echo ( $command-&amp;gt;render('Hello World!') );&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the PHP example provided above, class Bold, Italic works on the function render.&lt;br /&gt;
&lt;br /&gt;
In class Tag, all it does is for each command in the command object, render functionality is called, the render doesn't know which command, bold or italics is called and what doesn't knows the functionality of command (bold or italics) &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Applications&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Command pattern is used widely on in web based application frameworks like Java Struts from a PHP Perspective, usually in Action Classes, to allow framework users to define logic which responds to “events” in the application. Action Classes can be regarded as the “verbs” of the framework - they act on things - they do things.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Comparison to similar patterns&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Commands and Mementos have some similarity due to the fact they both work with an object's internal properties. The Command pattern keeps a record of changes to an object's state, and applies those changes in an ad-hoc fashion. A Memento also records changes to an object's state, and can restore that state at any time. The Chain of Responsibility pattern seems to handle processing in a similar manner to the Command, except it hands off processing to another process linearly unlike the Command pattern. An Interpreter pattern works in the example above because we are using language elements to determine which changes to apply at a given time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Advantages:&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	Since the object that implements the command design doesn't know what function it does and what object it affects, as it acts like a black box, the implementation of the objects that is calling the black box can be changed and it can be implemented independently without affecting the flow of the program.&lt;br /&gt;
&lt;br /&gt;
•	Hence command pattern gives a wide range of flexibility and sophisticated design features.&lt;br /&gt;
&amp;lt;b&amp;gt;Disadvantages:&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	It complicates the application design. Every pattern complicates application design; they’re used because the benefits outweigh the cost. In fact, they should only be used when the benefits outweigh the costs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://www.javaworld.com/javatips/jw-javatip68.html?page=1 Command Pattern on Javaworld]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;According to Javaworld,&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The design patterns not only accelerate the design phase of an object-oriented (OO) project but also increase the productivity of the development team and quality of the software. &lt;br /&gt;
&lt;br /&gt;
They describe a Command pattern as an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Sender     -  invokes an operation&lt;br /&gt;
Receiver   - receives the request to execute a certain operation. &lt;br /&gt;
Decoupling - the sender has no knowledge of the Receiver's interface&lt;br /&gt;
Request    - the command that is to be executed. &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Command pattern provides us flexibility as well as extensibility.&lt;br /&gt;
&lt;br /&gt;
The program discussed in the javaworld web directory is &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Fan {&lt;br /&gt;
        public void startRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void stopRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is not rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Light {&lt;br /&gt;
        public void turnOn( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is on &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void turnOff( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is off&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Switch {&lt;br /&gt;
        private Command UpCommand, DownCommand;&lt;br /&gt;
        public Switch( Command Up, Command Down) {&lt;br /&gt;
                UpCommand = Up; // concrete Command registers itself with the invoker &lt;br /&gt;
                DownCommand = Down;&lt;br /&gt;
        }&lt;br /&gt;
        void flipUp( ) { // invoker calls back concrete Command, which executes the Command on the receiver &lt;br /&gt;
                        UpCommand . execute ( ) ;                           &lt;br /&gt;
        }&lt;br /&gt;
        void flipDown( ) {&lt;br /&gt;
                        DownCommand . execute ( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOnCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOnCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOn( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOffCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOffCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOff( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOnCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOnCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . startRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOffCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOffCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . stopRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
public class TestCommand {&lt;br /&gt;
                public static void main(String[] args) {&lt;br /&gt;
                        Light  testLight = new Light( );&lt;br /&gt;
                        LightOnCommand testLOC = new LightOnCommand(testLight);&lt;br /&gt;
                        LightOffCommand testLFC = new LightOffCommand(testLight);&lt;br /&gt;
                        Switch testSwitch = new Switch( testLOC,testLFC);       &lt;br /&gt;
                        testSwitch.flipUp( );&lt;br /&gt;
                        testSwitch.flipDown( );&lt;br /&gt;
                        Fan testFan = new Fan( );&lt;br /&gt;
                        FanOnCommand foc = new FanOnCommand(testFan);&lt;br /&gt;
                        FanOffCommand ffc = new FanOffCommand(testFan);&lt;br /&gt;
                        Switch ts = new Switch( foc,ffc);&lt;br /&gt;
                        ts.flipUp( );&lt;br /&gt;
                        ts.flipDown( ); &lt;br /&gt;
                }&lt;br /&gt;
}               &lt;br /&gt;
Command.java&lt;br /&gt;
public interface Command {&lt;br /&gt;
        public abstract void execute ( );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Learning:&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	In the above example,&lt;br /&gt;
The class Fan has startRotate() and stopRotate() methods,the class Light has turnOn() and turnOff() methods.&lt;br /&gt;
The catch here is startRotate of Fan and turnOn of Light is performed when the switch is turned on and stopRotate of Fan and turnOff of Light is performed when the switch in turned off.&lt;br /&gt;
&lt;br /&gt;
•	Keeping this real world catch, the class switch is created as a kind of interface for &lt;br /&gt;
fan and light to work on it turn on and turn off requests.&lt;br /&gt;
&lt;br /&gt;
•	The switch has flipUp and flipDown methods which acts like command design implementation.&lt;br /&gt;
The switch doesn't know what action it performs on flipUp and flipDown and what object is affected when flipUp and flipDown is pressed. &lt;br /&gt;
&lt;br /&gt;
•	Hence flipUp and flipDown acts like a command without knowing what function it does and what object it affects, but eventually performing the required action. That is the beauty of command design. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;The Command pattern has the following advantages:&amp;lt;/b&amp;gt;&lt;br /&gt;
1.	A command decouples the object that invokes the operation from the one that knows how to perform it.&lt;br /&gt;
2.	A command is a first-class object. It can be manipulated and extended like any other object.&lt;br /&gt;
3.	Commands can be assembled into a composite command.&lt;br /&gt;
4.	It's easy to add new commands, because you don't have to change the existing classes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;References&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;http://www.itsmedia.ca/the-command-pattern-and-restful-interfaces/&lt;br /&gt;
&amp;lt;br&amp;gt;http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
&amp;lt;br&amp;gt;http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
&amp;lt;br&amp;gt;http://www.phpwact.org/&lt;br /&gt;
&amp;lt;br&amp;gt;http://www.phpwact.org/pattern/command&lt;br /&gt;
&amp;lt;br&amp;gt;http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
&amp;lt;br&amp;gt;http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
&amp;lt;br&amp;gt;http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
&amp;lt;br&amp;gt;http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html&lt;br /&gt;
&amp;lt;br&amp;gt;http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70584</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w45 pg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70584"/>
		<updated>2012-11-19T15:23:37Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Command Pattern =&lt;br /&gt;
&lt;br /&gt;
= Adapter Pattern, a directory of sites =&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Command Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Command Pattern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Introduction&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;[http://en.wikipedia.org/wiki/Command_pattern Definition:]&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command pattern is a behavioural design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
Three terms always associated with the command pattern are client, invoker and receiver. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Client&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The client instantiates the command object and provides the information required to call the method at a later time. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Invoker&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The invoker decides when the method should be called. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Receiver&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;How does it works?&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.&lt;br /&gt;
 &lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;&lt;br /&gt;
[http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html Command Real world Example ]&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parameterized with different requests. The &amp;quot;check&amp;quot; at a diner is an example of a Command pattern. The waiter or waitress takes an order, or command from a customer, and encapsulates that order by writing it on the check. The order is then queued for a short order cook. Note that the pad of &amp;quot;checks&amp;quot; used by different diners is not dependent on the menu, and therefore they can support commands to cook many different items.&lt;br /&gt;
&lt;br /&gt;
[[File:http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;When Would I Use This Pattern?&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The Command Pattern is useful when:&amp;lt;br&amp;gt;&lt;br /&gt;
•	A history of requests is needed&amp;lt;br&amp;gt;&lt;br /&gt;
•	You need callback functionality&amp;lt;br&amp;gt;&lt;br /&gt;
•	Requests need to be handled at variant times or in variant orders&amp;lt;br&amp;gt;&lt;br /&gt;
•	The invoker should be decoupled from the object handling the invocation.&amp;lt;br&amp;gt;&lt;br /&gt;
You'll see command being used a lot when you need to have multiple undo operations, where a stack of the recently executed commands are maintained. To implement the undo, all you need to do is get the last Command in the stack and execute it's undo() method.&lt;br /&gt;
You'll also find Command useful for wizards, progress bars, GUI buttons and menu actions, and other transactional behavior. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Directory of sites&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;[http://msdn.microsoft.com/en-us/magazine/cc163920.aspx Command pattern on MSDN Microsoft Website] &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Command Pattern to -Simplify Distributed System Design Using the Command Pattern, MSMQ, and .NET &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Object-based collaboration VS service-based collaboration&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	Communication or exchange of request and service and the format or the pattern based on play an important criteria in successful implementation of  object-based collaboration and service-oriented architecture. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
•	Systems generally share a common platform, allowing you to optimize in ways you can't on widely dispersed -systems that span various operating systems and development environments are preferred to follow object-based collaboration than service-based collaboration.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
•	But generally the participants are hybrid and they don't share a common platform,  in such situations service oriented architecture comes handy. Here the requests are queued in Microsoft Message Queuing (MSMQ) which implements Command design pattern. &lt;br /&gt;
&lt;br /&gt;
[[File:http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif]]&lt;br /&gt;
&lt;br /&gt;
The distributed command system discussed here is the combination of reliable messaging, services, and the Command pattern. While most often seen as a design pattern for implementing desktop application menu systems, the Command pattern offers advantages for distributed systems when combined with a robust messaging infrastructure like MSMQ. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Learning: &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The concept is very simple, in service oriented architecture, the participants don't share a common platform, hence you need a mechanism, a design pattern, a module to issue requests to objects.  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The module that is doing the requesting doesn’t know what the receiver will be, or what operation will be requested.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pattern and Participants&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class	              Description&lt;br /&gt;
Command	              Abstract base class for all Command objects&lt;br /&gt;
Concrete command      Any number of classes derived from Command&lt;br /&gt;
Client	              The creator of a command&lt;br /&gt;
Invoker	              Executes a command&lt;br /&gt;
Receiver	      Any class that is acted on by a command&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Another Example: &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In ASP.NET, the database communication are based on the commands, the database commands such as create connection, command statement, parameter doesn't know what the object is and what the object it is requesting and these are typical example of command design pattern it best.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;&lt;br /&gt;
Conclusion&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Command pattern specifies a set of interactions that you can apply to the development of distributed systems in .NET. Use this pattern in your application to implement a high-performance, extendable backbone connecting your near link nodes that run .NET-enabled operating systems to achieve optimal performance and rapid development. Distributed commands are a mix of classic object orientation, messaging, and service architecture that together fill a definite need in the distributed systems space.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture Advantages]&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefit of the Command class is that it makes it easier to add new functions to an application. &lt;br /&gt;
If we add a new capability, we no longer need to reopen the Controller class to code routing information. &lt;br /&gt;
Instead, we encapsulate the information in a Command object and assign it to whatever control we use in the View to invoke the new capability. &lt;br /&gt;
When the Command gets passed to the Controller, the Controller can use it to route the request, without knowing anything about it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Disadvantages:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.phpwact.org/ Command Pattern on Web Application Component Toolkit (WACT)]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.phpwact.org/pattern/command Web Application Component Toolkit]&lt;br /&gt;
The Web Application Component Toolkit is a framework for creating web applications. WACT facilitates a modular approach where individual, independent or reusable components may be integrated into a larger web application. WACT assists in implementing the Model View Controller pattern and the related Domain Model, Template View, Front Controller and Application Controller patterns. WACT emphasizes writing secure web applications.  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;WACT sees Command Pattern as&amp;lt;/b&amp;gt;&lt;br /&gt;
A GoF Design Pattern with the intent: “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”&lt;br /&gt;
&lt;br /&gt;
In WACT, this is seen with the FormController, where users can define a class which must contain the method performAction();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class AddressForm extends PostFormController {&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
 &lt;br /&gt;
    function InitializeActions() {&lt;br /&gt;
        $this-&amp;gt;registerSubmitAction(&amp;quot;register&amp;quot;, &amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
        $this-&amp;gt;registerDefaultSubmitAction(&amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The registerSubmitAction call above points the controller at the class RegisterUser, which is where the Command pattern resides.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class RegisterUser {&lt;br /&gt;
 &lt;br /&gt;
    function performAction(&amp;amp;$context) {&lt;br /&gt;
        $DataSpace =&amp;amp; $context-&amp;gt;getDataSpace();&lt;br /&gt;
        $Page =&amp;amp; new Template('/input/thanks.html');&lt;br /&gt;
        $Page-&amp;gt;import($DataSpace-&amp;gt;export());&lt;br /&gt;
        $Page-&amp;gt;display();&lt;br /&gt;
        return FORM_COMPLETE;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The FormController expects to find the performAction method in the Action Class RegisterUser, calling it when the form is submitted (and successfully validated).&lt;br /&gt;
&lt;br /&gt;
The Command pattern is a Gang of Four Design Pattern, used to allow a function name to be wrapped up in a class namespace, so that some code, which will call the function, can be given an instance of one of many classes without caring which which instance it’s dealing with.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Learning:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Here the Form controller in its post functionality exhibits command pattern as it requires the object that calls the post form functionality to implement performAction method, the actions that the object needs to perform when the form is submitted, but the post form controller doesn't know what are actions performed by the performAction method and whose performAction  method is called all it requires is it needs a class that has the performAction method to post it forms. &lt;br /&gt;
Hence the performAction acts like a command that is being called whenever the form is posted.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PHP example &lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
/* interface */ class Tag {&lt;br /&gt;
   // The abstract command method&lt;br /&gt;
   function render($content);&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Bold {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;b&amp;gt;$content&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Italic {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;i&amp;gt;$content&amp;lt;/i&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
$commands = array (&lt;br /&gt;
    new Bold(),new Italic(), new Italic(), new Bold()&lt;br /&gt;
);&lt;br /&gt;
 &lt;br /&gt;
// Iterate over the command objects&lt;br /&gt;
foreach ( $commands as $command ) {&lt;br /&gt;
    echo ( $command-&amp;gt;render('Hello World!') );&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the PHP example provided above, class Bold, Italic works on the function render.&lt;br /&gt;
&lt;br /&gt;
In class Tag, all it does is for each command in the command object, render functionality is called, the render doesn't know which command, bold or italics is called and what doesn't knows the functionality of command (bold or italics) &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Applications&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Command pattern is used widely on in web based application frameworks like Java Struts from a PHP Perspective, usually in Action Classes, to allow framework users to define logic which responds to “events” in the application. Action Classes can be regarded as the “verbs” of the framework - they act on things - they do things.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Comparison to similar patterns&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Commands and Mementos have some similarity due to the fact they both work with an object's internal properties. The Command pattern keeps a record of changes to an object's state, and applies those changes in an ad-hoc fashion. A Memento also records changes to an object's state, and can restore that state at any time. The Chain of Responsibility pattern seems to handle processing in a similar manner to the Command, except it hands off processing to another process linearly unlike the Command pattern. An Interpreter pattern works in the example above because we are using language elements to determine which changes to apply at a given time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Advantages:&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the object that implements the command design doesn't know what function it does and what object it affects, as it acts like a black box, the implementation of the objects that is calling the black box can be changed and it can be implemented independently without affecting the flow of the program.&lt;br /&gt;
&lt;br /&gt;
Hence command pattern gives a wide range of flexibility and sophisticated design features.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
It complicates the application design. Every pattern complicates application design; they’re used because the benefits outweigh the cost. In fact, they should only be used when the benefits outweigh the costs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://www.javaworld.com/javatips/jw-javatip68.html?page=1 Command Pattern on Javaworld]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;According to Javaworld,&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The design patterns not only accelerate the design phase of an object-oriented (OO) project but also increase the productivity of the development team and quality of the software. &lt;br /&gt;
&lt;br /&gt;
They describe a Command pattern as an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Sender     -  invokes an operation&lt;br /&gt;
Receiver   - receives the request to execute a certain operation. &lt;br /&gt;
Decoupling - the sender has no knowledge of the Receiver's interface&lt;br /&gt;
Request    - the command that is to be executed. &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Command pattern provides us flexibility as well as extensibility.&lt;br /&gt;
&lt;br /&gt;
The program discussed in the javaworld web directory is &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Fan {&lt;br /&gt;
        public void startRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void stopRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is not rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Light {&lt;br /&gt;
        public void turnOn( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is on &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void turnOff( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is off&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Switch {&lt;br /&gt;
        private Command UpCommand, DownCommand;&lt;br /&gt;
        public Switch( Command Up, Command Down) {&lt;br /&gt;
                UpCommand = Up; // concrete Command registers itself with the invoker &lt;br /&gt;
                DownCommand = Down;&lt;br /&gt;
        }&lt;br /&gt;
        void flipUp( ) { // invoker calls back concrete Command, which executes the Command on the receiver &lt;br /&gt;
                        UpCommand . execute ( ) ;                           &lt;br /&gt;
        }&lt;br /&gt;
        void flipDown( ) {&lt;br /&gt;
                        DownCommand . execute ( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOnCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOnCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOn( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOffCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOffCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOff( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOnCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOnCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . startRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOffCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOffCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . stopRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
public class TestCommand {&lt;br /&gt;
                public static void main(String[] args) {&lt;br /&gt;
                        Light  testLight = new Light( );&lt;br /&gt;
                        LightOnCommand testLOC = new LightOnCommand(testLight);&lt;br /&gt;
                        LightOffCommand testLFC = new LightOffCommand(testLight);&lt;br /&gt;
                        Switch testSwitch = new Switch( testLOC,testLFC);       &lt;br /&gt;
                        testSwitch.flipUp( );&lt;br /&gt;
                        testSwitch.flipDown( );&lt;br /&gt;
                        Fan testFan = new Fan( );&lt;br /&gt;
                        FanOnCommand foc = new FanOnCommand(testFan);&lt;br /&gt;
                        FanOffCommand ffc = new FanOffCommand(testFan);&lt;br /&gt;
                        Switch ts = new Switch( foc,ffc);&lt;br /&gt;
                        ts.flipUp( );&lt;br /&gt;
                        ts.flipDown( ); &lt;br /&gt;
                }&lt;br /&gt;
}               &lt;br /&gt;
Command.java&lt;br /&gt;
public interface Command {&lt;br /&gt;
        public abstract void execute ( );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Learning:&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example,&lt;br /&gt;
The class Fan has startRotate() and stopRotate() methods,the class Light has turnOn() and turnOff() methods.&lt;br /&gt;
The catch here is startRotate of Fan and turnOn of Light is performed when the switch is turned on and stopRotate of Fan and turnOff of Light is performed when the switch in turned off.&lt;br /&gt;
&lt;br /&gt;
Keeping this real world catch, the class switch is created as a kind of interface for &lt;br /&gt;
fan and light to work on it turn on and turn off requests.&lt;br /&gt;
&lt;br /&gt;
The switch has flipUp and flipDown methods which acts like command design implementation.&lt;br /&gt;
The switch doesn't know what action it performs on flipUp and flipDown and what object is affected when flipUp and flipDown is pressed. &lt;br /&gt;
&lt;br /&gt;
Hence flipUp and flipDown acts like a command without knowing what function it does and what object it affects, but eventually performing the required action. That is the beauty of command design. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;The Command pattern has the following advantages:&amp;lt;/b&amp;gt;&lt;br /&gt;
1.	A command decouples the object that invokes the operation from the one that knows how to perform it.&lt;br /&gt;
2.	A command is a first-class object. It can be manipulated and extended like any other object.&lt;br /&gt;
3.	Commands can be assembled into a composite command.&lt;br /&gt;
4.	It's easy to add new commands, because you don't have to change the existing classes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;References&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;http://www.itsmedia.ca/the-command-pattern-and-restful-interfaces/&lt;br /&gt;
&amp;lt;br&amp;gt;http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
&amp;lt;br&amp;gt;http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
&amp;lt;br&amp;gt;http://www.phpwact.org/&lt;br /&gt;
&amp;lt;br&amp;gt;http://www.phpwact.org/pattern/command&lt;br /&gt;
&amp;lt;br&amp;gt;http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
&amp;lt;br&amp;gt;http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
&amp;lt;br&amp;gt;http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
&amp;lt;br&amp;gt;http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html&lt;br /&gt;
&amp;lt;br&amp;gt;http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70582</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w45 pg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70582"/>
		<updated>2012-11-19T15:17:55Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Command Pattern =&lt;br /&gt;
&lt;br /&gt;
= Adapter Pattern, a directory of sites =&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Command Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Command Pattern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Introduction&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;[http://en.wikipedia.org/wiki/Command_pattern Definition:]&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command pattern is a behavioural design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
Three terms always associated with the command pattern are client, invoker and receiver. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Client&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The client instantiates the command object and provides the information required to call the method at a later time. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Invoker&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The invoker decides when the method should be called. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Receiver&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;How does it works?&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.&lt;br /&gt;
 &lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;&lt;br /&gt;
[http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html Command Real world Example ]&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parameterized with different requests. The &amp;quot;check&amp;quot; at a diner is an example of a Command pattern. The waiter or waitress takes an order, or command from a customer, and encapsulates that order by writing it on the check. The order is then queued for a short order cook. Note that the pad of &amp;quot;checks&amp;quot; used by different diners is not dependent on the menu, and therefore they can support commands to cook many different items.&lt;br /&gt;
&lt;br /&gt;
[[File:http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;When Would I Use This Pattern?&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The Command Pattern is useful when:&amp;lt;br&amp;gt;&lt;br /&gt;
•	A history of requests is needed&amp;lt;br&amp;gt;&lt;br /&gt;
•	You need callback functionality&amp;lt;br&amp;gt;&lt;br /&gt;
•	Requests need to be handled at variant times or in variant orders&amp;lt;br&amp;gt;&lt;br /&gt;
•	The invoker should be decoupled from the object handling the invocation.&amp;lt;br&amp;gt;&lt;br /&gt;
You'll see command being used a lot when you need to have multiple undo operations, where a stack of the recently executed commands are maintained. To implement the undo, all you need to do is get the last Command in the stack and execute it's undo() method.&lt;br /&gt;
You'll also find Command useful for wizards, progress bars, GUI buttons and menu actions, and other transactional behavior. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Directory of sites&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;[http://msdn.microsoft.com/en-us/magazine/cc163920.aspx Command pattern on MSDN Microsoft Website] &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Command Pattern to -Simplify Distributed System Design Using the Command Pattern, MSMQ, and .NET &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Object-based collaboration VS service-based collaboration&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	Communication or exchange of request and service and the format or the pattern based on play an important criteria in successful implementation of  object-based collaboration and service-oriented architecture. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
•	Systems generally share a common platform, allowing you to optimize in ways you can't on widely dispersed -systems that span various operating systems and development environments are preferred to follow object-based collaboration than service-based collaboration.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
•	But generally the participants are hybrid and they don't share a common platform,  in such situations service oriented architecture comes handy. Here the requests are queued in Microsoft Message Queuing (MSMQ) which implements Command design pattern. &lt;br /&gt;
&lt;br /&gt;
[[File:http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif]]&lt;br /&gt;
&lt;br /&gt;
The distributed command system discussed here is the combination of reliable messaging, services, and the Command pattern. While most often seen as a design pattern for implementing desktop application menu systems, the Command pattern offers advantages for distributed systems when combined with a robust messaging infrastructure like MSMQ. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Learning: &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The concept is very simple, in service oriented architecture, the participants don't share a common platform, hence you need a mechanism, a design pattern, a module to issue requests to objects.  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The module that is doing the requesting doesn’t know what the receiver will be, or what operation will be requested.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pattern and Participants&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class	              Description&lt;br /&gt;
Command	              Abstract base class for all Command objects&lt;br /&gt;
Concrete command      Any number of classes derived from Command&lt;br /&gt;
Client	              The creator of a command&lt;br /&gt;
Invoker	              Executes a command&lt;br /&gt;
Receiver	      Any class that is acted on by a command&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Another Example: &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In ASP.NET, the database communication are based on the commands, the database commands such as create connection, command statement, parameter doesn't know what the object is and what the object it is requesting and these are typical example of command design pattern it best.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;&lt;br /&gt;
Conclusion&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Command pattern specifies a set of interactions that you can apply to the development of distributed systems in .NET. Use this pattern in your application to implement a high-performance, extendable backbone connecting your near link nodes that run .NET-enabled operating systems to achieve optimal performance and rapid development. Distributed commands are a mix of classic object orientation, messaging, and service architecture that together fill a definite need in the distributed systems space.&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture Advantages]&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefit of the Command class is that it makes it easier to add new functions to an application. &lt;br /&gt;
If we add a new capability, we no longer need to reopen the Controller class to code routing information. &lt;br /&gt;
Instead, we encapsulate the information in a Command object and assign it to whatever control we use in the View to invoke the new capability. &lt;br /&gt;
When the Command gets passed to the Controller, the Controller can use it to route the request, without knowing anything about it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;br&amp;gt;Disadvantages:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.phpwact.org/ Command Pattern on Web Application Component Toolkit (WACT)]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.phpwact.org/pattern/command Web Application Component Toolkit]&lt;br /&gt;
The Web Application Component Toolkit is a framework for creating web applications. WACT facilitates a modular approach where individual, independent or reusable components may be integrated into a larger web application. WACT assists in implementing the Model View Controller pattern and the related Domain Model, Template View, Front Controller and Application Controller patterns. WACT emphasizes writing secure web applications.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;WACT sees Command Pattern as&amp;lt;/b&amp;gt;&lt;br /&gt;
A GoF Design Pattern with the intent: “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”&lt;br /&gt;
&lt;br /&gt;
In WACT, this is seen with the FormController, where users can define a class which must contain the method performAction();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class AddressForm extends PostFormController {&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
 &lt;br /&gt;
    function InitializeActions() {&lt;br /&gt;
        $this-&amp;gt;registerSubmitAction(&amp;quot;register&amp;quot;, &amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
        $this-&amp;gt;registerDefaultSubmitAction(&amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The registerSubmitAction call above points the controller at the class RegisterUser, which is where the Command pattern resides.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class RegisterUser {&lt;br /&gt;
 &lt;br /&gt;
    function performAction(&amp;amp;$context) {&lt;br /&gt;
        $DataSpace =&amp;amp; $context-&amp;gt;getDataSpace();&lt;br /&gt;
        $Page =&amp;amp; new Template('/input/thanks.html');&lt;br /&gt;
        $Page-&amp;gt;import($DataSpace-&amp;gt;export());&lt;br /&gt;
        $Page-&amp;gt;display();&lt;br /&gt;
        return FORM_COMPLETE;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The FormController expects to find the performAction method in the Action Class RegisterUser, calling it when the form is submitted (and successfully validated).&lt;br /&gt;
&lt;br /&gt;
The Command pattern is a Gang of Four Design Pattern, used to allow a function name to be wrapped up in a class namespace, so that some code, which will call the function, can be given an instance of one of many classes without caring which which instance it’s dealing with.&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
Here the Form controller in its post functionality exhibits command pattern as it requires the object that calls the post form functionality to implement performAction method, the actions that the object needs to perform when the form is submitted, but the post form controller doesn't know what are actions performed by the performAction method and whose performAction  method is called all it requires is it needs a class that has the performAction method to post it forms. &lt;br /&gt;
Hence the performAction acts like a command that is being called whenever the form is posted.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example:&lt;br /&gt;
PHP example &lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
/* interface */ class Tag {&lt;br /&gt;
   // The abstract command method&lt;br /&gt;
   function render($content);&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Bold {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;b&amp;gt;$content&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Italic {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;i&amp;gt;$content&amp;lt;/i&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
$commands = array (&lt;br /&gt;
    new Bold(),new Italic(), new Italic(), new Bold()&lt;br /&gt;
);&lt;br /&gt;
 &lt;br /&gt;
// Iterate over the command objects&lt;br /&gt;
foreach ( $commands as $command ) {&lt;br /&gt;
    echo ( $command-&amp;gt;render('Hello World!') );&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the PHP example provided above, class Bold, Italic works on the function render.&lt;br /&gt;
&lt;br /&gt;
In class Tag, all it does is for each command in the command object, render functionality is called, the render doesn't know which command, bold or italics is called and what doesn't knows the functionality of command (bold or italics) &lt;br /&gt;
&lt;br /&gt;
Applications&lt;br /&gt;
&lt;br /&gt;
The Command pattern is used widely on in web based application frameworks like Java Struts from a PHP Perspective, usually in Action Classes, to allow framework users to define logic which responds to “events” in the application. Action Classes can be regarded as the “verbs” of the framework - they act on things - they do things.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Comparison to similar patterns&lt;br /&gt;
&lt;br /&gt;
Commands and Mementos have some similarity due to the fact they both work with an object's internal properties. The Command pattern keeps a record of changes to an object's state, and applies those changes in an ad-hoc fashion. A Memento also records changes to an object's state, and can restore that state at any time. The Chain of Responsibility pattern seems to handle processing in a similar manner to the Command, except it hands off processing to another process linearly unlike the Command pattern. An Interpreter pattern works in the example above because we are using language elements to determine which changes to apply at a given time.&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
Since the object that implements the command design doesn't know what function it does and what object it affects, as it acts like a black box, the implementation of the objects that is calling the black box can be changed and it can be implemented independently without affecting the flow of the program.&lt;br /&gt;
&lt;br /&gt;
Hence command pattern gives a wide range of flexibility and sophisticated design features.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
It complicates the application design. Every pattern complicates application design; they’re used because the benefits outweigh the cost. In fact, they should only be used when the benefits outweigh the costs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Javaworld&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
&lt;br /&gt;
According to Javaworld,&lt;br /&gt;
&lt;br /&gt;
The design patterns not only accelerate the design phase of an object-oriented (OO) project but also increase the productivity of the development team and quality of the software. &lt;br /&gt;
&lt;br /&gt;
They describe a Command pattern as an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. &lt;br /&gt;
&lt;br /&gt;
Sender -  invokes an operation&lt;br /&gt;
Receiver - receives the request to execute a certain operation. &lt;br /&gt;
Decoupling - the sender has no knowledge of the Receiver's interface&lt;br /&gt;
Request  - the command that is to be executed. &lt;br /&gt;
&lt;br /&gt;
Command pattern provides us flexibility as well as extensibility.&lt;br /&gt;
&lt;br /&gt;
The program discussed in the javaworld web directory is class Fan {&lt;br /&gt;
        public void startRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void stopRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is not rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Light {&lt;br /&gt;
        public void turnOn( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is on &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void turnOff( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is off&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Switch {&lt;br /&gt;
        private Command UpCommand, DownCommand;&lt;br /&gt;
        public Switch( Command Up, Command Down) {&lt;br /&gt;
                UpCommand = Up; // concrete Command registers itself with the invoker &lt;br /&gt;
                DownCommand = Down;&lt;br /&gt;
        }&lt;br /&gt;
        void flipUp( ) { // invoker calls back concrete Command, which executes the Command on the receiver &lt;br /&gt;
                        UpCommand . execute ( ) ;                           &lt;br /&gt;
        }&lt;br /&gt;
        void flipDown( ) {&lt;br /&gt;
                        DownCommand . execute ( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOnCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOnCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOn( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOffCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOffCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOff( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOnCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOnCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . startRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOffCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOffCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . stopRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
public class TestCommand {&lt;br /&gt;
                public static void main(String[] args) {&lt;br /&gt;
                        Light  testLight = new Light( );&lt;br /&gt;
                        LightOnCommand testLOC = new LightOnCommand(testLight);&lt;br /&gt;
                        LightOffCommand testLFC = new LightOffCommand(testLight);&lt;br /&gt;
                        Switch testSwitch = new Switch( testLOC,testLFC);       &lt;br /&gt;
                        testSwitch.flipUp( );&lt;br /&gt;
                        testSwitch.flipDown( );&lt;br /&gt;
                        Fan testFan = new Fan( );&lt;br /&gt;
                        FanOnCommand foc = new FanOnCommand(testFan);&lt;br /&gt;
                        FanOffCommand ffc = new FanOffCommand(testFan);&lt;br /&gt;
                        Switch ts = new Switch( foc,ffc);&lt;br /&gt;
                        ts.flipUp( );&lt;br /&gt;
                        ts.flipDown( ); &lt;br /&gt;
                }&lt;br /&gt;
}               &lt;br /&gt;
Command.java&lt;br /&gt;
public interface Command {&lt;br /&gt;
        public abstract void execute ( );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the above example,&lt;br /&gt;
The class Fan has startRotate() and stopRotate() methods,the class Light has turnOn() and turnOff() methods.&lt;br /&gt;
The catch here is startRotate of Fan and turnOn of Light is performed when the switch is turned on and stopRotate of Fan and turnOff of Light is performed when the switch in turned off.&lt;br /&gt;
&lt;br /&gt;
Keeping this real world catch, the class switch is created as a kind of interface for &lt;br /&gt;
fan and light to work on it turn on and turn off requests.&lt;br /&gt;
&lt;br /&gt;
The switch has flipUp and flipDown methods which acts like command design implementation.&lt;br /&gt;
The switch doesn't know what action it performs on flipUp and flipDown and what object is affected when flipUp and flipDown is pressed. &lt;br /&gt;
&lt;br /&gt;
Hence flipUp and flipDown acts like a command without knowing what function it does and what object it affects, but eventually performing the required action. That is the beauty of command design. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Command pattern has the following advantages:&lt;br /&gt;
1.	A command decouples the object that invokes the operation from the one that knows how to perform it.&lt;br /&gt;
2.	A command is a first-class object. It can be manipulated and extended like any other object.&lt;br /&gt;
3.	Commands can be assembled into a composite command.&lt;br /&gt;
4.	It's easy to add new commands, because you don't have to change the existing classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References&lt;br /&gt;
http://www.itsmedia.ca/the-command-pattern-and-restful-interfaces/&lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70581</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w45 pg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70581"/>
		<updated>2012-11-19T15:15:44Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Command Pattern =&lt;br /&gt;
&lt;br /&gt;
= Adapter Pattern, a directory of sites =&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Command Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Command Pattern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Introduction&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;[http://en.wikipedia.org/wiki/Command_pattern Definition:]&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command pattern is a behavioural design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
Three terms always associated with the command pattern are client, invoker and receiver. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Client&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The client instantiates the command object and provides the information required to call the method at a later time. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Invoker&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The invoker decides when the method should be called. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Receiver&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;How does it works?&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.&lt;br /&gt;
 &lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;&lt;br /&gt;
[http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html Command Real world Example ]&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parameterized with different requests. The &amp;quot;check&amp;quot; at a diner is an example of a Command pattern. The waiter or waitress takes an order, or command from a customer, and encapsulates that order by writing it on the check. The order is then queued for a short order cook. Note that the pad of &amp;quot;checks&amp;quot; used by different diners is not dependent on the menu, and therefore they can support commands to cook many different items.&lt;br /&gt;
&lt;br /&gt;
[[File:http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;When Would I Use This Pattern?&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The Command Pattern is useful when:&amp;lt;br&amp;gt;&lt;br /&gt;
•	A history of requests is needed&amp;lt;br&amp;gt;&lt;br /&gt;
•	You need callback functionality&amp;lt;br&amp;gt;&lt;br /&gt;
•	Requests need to be handled at variant times or in variant orders&amp;lt;br&amp;gt;&lt;br /&gt;
•	The invoker should be decoupled from the object handling the invocation.&amp;lt;br&amp;gt;&lt;br /&gt;
You'll see command being used a lot when you need to have multiple undo operations, where a stack of the recently executed commands are maintained. To implement the undo, all you need to do is get the last Command in the stack and execute it's undo() method.&lt;br /&gt;
You'll also find Command useful for wizards, progress bars, GUI buttons and menu actions, and other transactional behavior. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Directory of sites&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;[http://msdn.microsoft.com/en-us/magazine/cc163920.aspx Command pattern on MSDN Microsoft Website] &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Command Pattern to -Simplify Distributed System Design Using the Command Pattern, MSMQ, and .NET &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Object-based collaboration VS service-based collaboration&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	Communication or exchange of request and service and the format or the pattern based on play an important criteria in successful implementation of  object-based collaboration and service-oriented architecture. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
•	Systems generally share a common platform, allowing you to optimize in ways you can't on widely dispersed -systems that span various operating systems and development environments are preferred to follow object-based collaboration than service-based collaboration.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
•	But generally the participants are hybrid and they don't share a common platform,  in such situations service oriented architecture comes handy. Here the requests are queued in Microsoft Message Queuing (MSMQ) which implements Command design pattern. &lt;br /&gt;
&lt;br /&gt;
[[File:http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif]]&lt;br /&gt;
&lt;br /&gt;
The distributed command system discussed here is the combination of reliable messaging, services, and the Command pattern. While most often seen as a design pattern for implementing desktop application menu systems, the Command pattern offers advantages for distributed systems when combined with a robust messaging infrastructure like MSMQ. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Learning: &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The concept is very simple, in service oriented architecture, the participants don't share a common platform, hence you need a mechanism, a design pattern, a module to issue requests to objects.  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The module that is doing the requesting doesn’t know what the receiver will be, or what operation will be requested.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pattern and Participants&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class	Description&lt;br /&gt;
Command	Abstract base class for all Command objects&lt;br /&gt;
Concrete command	Any number of classes derived from Command&lt;br /&gt;
Client	The creator of a command&lt;br /&gt;
Invoker	Executes a command&lt;br /&gt;
Receiver	Any class that is acted on by a command&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Another Example: &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In ASP.NET, the database communication are based on the commands, the database commands such as create connection, command statement, parameter doesn't know what the object is and what the object it is requesting and these are typical example of command design pattern it best.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;&lt;br /&gt;
Conclusion&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The Command pattern specifies a set of interactions that you can apply to the development of distributed systems in .NET. Use this pattern in your application to implement a high-performance, extendable backbone connecting your near link nodes that run .NET-enabled operating systems to achieve optimal performance and rapid development. Distributed commands are a mix of classic object orientation, messaging, and service architecture that together fill a definite need in the distributed systems space.&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture Advantages]&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefit of the Command class is that it makes it easier to add new functions to an application. &lt;br /&gt;
If we add a new capability, we no longer need to reopen the Controller class to code routing information. &lt;br /&gt;
Instead, we encapsulate the information in a Command object and assign it to whatever control we use in the View to invoke the new capability. &lt;br /&gt;
When the Command gets passed to the Controller, the Controller can use it to route the request, without knowing anything about it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;br&amp;gt;Disadvantages:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.phpwact.org/ Command Pattern on Web Application Component Toolkit (WACT)]&lt;br /&gt;
&lt;br /&gt;
[http://www.phpwact.org/pattern/command Web Application Component Toolkit]&lt;br /&gt;
The Web Application Component Toolkit is a framework for creating web applications. WACT facilitates a modular approach where individual, independent or reusable components may be integrated into a larger web application. WACT assists in implementing the Model View Controller pattern and the related Domain Model, Template View, Front Controller and Application Controller patterns. WACT emphasizes writing secure web applications.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
WACT sees Command Pattern as&lt;br /&gt;
A GoF Design Pattern with the intent: “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”&lt;br /&gt;
&lt;br /&gt;
In WACT, this is seen with the FormController, where users can define a class which must contain the method performAction();&lt;br /&gt;
&lt;br /&gt;
class AddressForm extends PostFormController {&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
 &lt;br /&gt;
    function InitializeActions() {&lt;br /&gt;
        $this-&amp;gt;registerSubmitAction(&amp;quot;register&amp;quot;, &amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
        $this-&amp;gt;registerDefaultSubmitAction(&amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The registerSubmitAction call above points the controller at the class RegisterUser, which is where the Command pattern resides.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class RegisterUser {&lt;br /&gt;
 &lt;br /&gt;
    function performAction(&amp;amp;$context) {&lt;br /&gt;
        $DataSpace =&amp;amp; $context-&amp;gt;getDataSpace();&lt;br /&gt;
        $Page =&amp;amp; new Template('/input/thanks.html');&lt;br /&gt;
        $Page-&amp;gt;import($DataSpace-&amp;gt;export());&lt;br /&gt;
        $Page-&amp;gt;display();&lt;br /&gt;
        return FORM_COMPLETE;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The FormController expects to find the performAction method in the Action Class RegisterUser, calling it when the form is submitted (and successfully validated).&lt;br /&gt;
&lt;br /&gt;
The Command pattern is a Gang of Four Design Pattern, used to allow a function name to be wrapped up in a class namespace, so that some code, which will call the function, can be given an instance of one of many classes without caring which which instance it’s dealing with.&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
Here the Form controller in its post functionality exhibits command pattern as it requires the object that calls the post form functionality to implement performAction method, the actions that the object needs to perform when the form is submitted, but the post form controller doesn't know what are actions performed by the performAction method and whose performAction  method is called all it requires is it needs a class that has the performAction method to post it forms. &lt;br /&gt;
Hence the performAction acts like a command that is being called whenever the form is posted.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example:&lt;br /&gt;
PHP example &lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
/* interface */ class Tag {&lt;br /&gt;
   // The abstract command method&lt;br /&gt;
   function render($content);&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Bold {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;b&amp;gt;$content&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Italic {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;i&amp;gt;$content&amp;lt;/i&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
$commands = array (&lt;br /&gt;
    new Bold(),new Italic(), new Italic(), new Bold()&lt;br /&gt;
);&lt;br /&gt;
 &lt;br /&gt;
// Iterate over the command objects&lt;br /&gt;
foreach ( $commands as $command ) {&lt;br /&gt;
    echo ( $command-&amp;gt;render('Hello World!') );&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the PHP example provided above, class Bold, Italic works on the function render.&lt;br /&gt;
&lt;br /&gt;
In class Tag, all it does is for each command in the command object, render functionality is called, the render doesn't know which command, bold or italics is called and what doesn't knows the functionality of command (bold or italics) &lt;br /&gt;
&lt;br /&gt;
Applications&lt;br /&gt;
&lt;br /&gt;
The Command pattern is used widely on in web based application frameworks like Java Struts from a PHP Perspective, usually in Action Classes, to allow framework users to define logic which responds to “events” in the application. Action Classes can be regarded as the “verbs” of the framework - they act on things - they do things.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Comparison to similar patterns&lt;br /&gt;
&lt;br /&gt;
Commands and Mementos have some similarity due to the fact they both work with an object's internal properties. The Command pattern keeps a record of changes to an object's state, and applies those changes in an ad-hoc fashion. A Memento also records changes to an object's state, and can restore that state at any time. The Chain of Responsibility pattern seems to handle processing in a similar manner to the Command, except it hands off processing to another process linearly unlike the Command pattern. An Interpreter pattern works in the example above because we are using language elements to determine which changes to apply at a given time.&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
Since the object that implements the command design doesn't know what function it does and what object it affects, as it acts like a black box, the implementation of the objects that is calling the black box can be changed and it can be implemented independently without affecting the flow of the program.&lt;br /&gt;
&lt;br /&gt;
Hence command pattern gives a wide range of flexibility and sophisticated design features.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
It complicates the application design. Every pattern complicates application design; they’re used because the benefits outweigh the cost. In fact, they should only be used when the benefits outweigh the costs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Javaworld&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
&lt;br /&gt;
According to Javaworld,&lt;br /&gt;
&lt;br /&gt;
The design patterns not only accelerate the design phase of an object-oriented (OO) project but also increase the productivity of the development team and quality of the software. &lt;br /&gt;
&lt;br /&gt;
They describe a Command pattern as an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. &lt;br /&gt;
&lt;br /&gt;
Sender -  invokes an operation&lt;br /&gt;
Receiver - receives the request to execute a certain operation. &lt;br /&gt;
Decoupling - the sender has no knowledge of the Receiver's interface&lt;br /&gt;
Request  - the command that is to be executed. &lt;br /&gt;
&lt;br /&gt;
Command pattern provides us flexibility as well as extensibility.&lt;br /&gt;
&lt;br /&gt;
The program discussed in the javaworld web directory is class Fan {&lt;br /&gt;
        public void startRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void stopRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is not rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Light {&lt;br /&gt;
        public void turnOn( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is on &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void turnOff( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is off&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Switch {&lt;br /&gt;
        private Command UpCommand, DownCommand;&lt;br /&gt;
        public Switch( Command Up, Command Down) {&lt;br /&gt;
                UpCommand = Up; // concrete Command registers itself with the invoker &lt;br /&gt;
                DownCommand = Down;&lt;br /&gt;
        }&lt;br /&gt;
        void flipUp( ) { // invoker calls back concrete Command, which executes the Command on the receiver &lt;br /&gt;
                        UpCommand . execute ( ) ;                           &lt;br /&gt;
        }&lt;br /&gt;
        void flipDown( ) {&lt;br /&gt;
                        DownCommand . execute ( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOnCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOnCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOn( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOffCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOffCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOff( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOnCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOnCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . startRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOffCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOffCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . stopRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
public class TestCommand {&lt;br /&gt;
                public static void main(String[] args) {&lt;br /&gt;
                        Light  testLight = new Light( );&lt;br /&gt;
                        LightOnCommand testLOC = new LightOnCommand(testLight);&lt;br /&gt;
                        LightOffCommand testLFC = new LightOffCommand(testLight);&lt;br /&gt;
                        Switch testSwitch = new Switch( testLOC,testLFC);       &lt;br /&gt;
                        testSwitch.flipUp( );&lt;br /&gt;
                        testSwitch.flipDown( );&lt;br /&gt;
                        Fan testFan = new Fan( );&lt;br /&gt;
                        FanOnCommand foc = new FanOnCommand(testFan);&lt;br /&gt;
                        FanOffCommand ffc = new FanOffCommand(testFan);&lt;br /&gt;
                        Switch ts = new Switch( foc,ffc);&lt;br /&gt;
                        ts.flipUp( );&lt;br /&gt;
                        ts.flipDown( ); &lt;br /&gt;
                }&lt;br /&gt;
}               &lt;br /&gt;
Command.java&lt;br /&gt;
public interface Command {&lt;br /&gt;
        public abstract void execute ( );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the above example,&lt;br /&gt;
The class Fan has startRotate() and stopRotate() methods,the class Light has turnOn() and turnOff() methods.&lt;br /&gt;
The catch here is startRotate of Fan and turnOn of Light is performed when the switch is turned on and stopRotate of Fan and turnOff of Light is performed when the switch in turned off.&lt;br /&gt;
&lt;br /&gt;
Keeping this real world catch, the class switch is created as a kind of interface for &lt;br /&gt;
fan and light to work on it turn on and turn off requests.&lt;br /&gt;
&lt;br /&gt;
The switch has flipUp and flipDown methods which acts like command design implementation.&lt;br /&gt;
The switch doesn't know what action it performs on flipUp and flipDown and what object is affected when flipUp and flipDown is pressed. &lt;br /&gt;
&lt;br /&gt;
Hence flipUp and flipDown acts like a command without knowing what function it does and what object it affects, but eventually performing the required action. That is the beauty of command design. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Command pattern has the following advantages:&lt;br /&gt;
1.	A command decouples the object that invokes the operation from the one that knows how to perform it.&lt;br /&gt;
2.	A command is a first-class object. It can be manipulated and extended like any other object.&lt;br /&gt;
3.	Commands can be assembled into a composite command.&lt;br /&gt;
4.	It's easy to add new commands, because you don't have to change the existing classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References&lt;br /&gt;
http://www.itsmedia.ca/the-command-pattern-and-restful-interfaces/&lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70580</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w45 pg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70580"/>
		<updated>2012-11-19T15:14:16Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Command Pattern =&lt;br /&gt;
&lt;br /&gt;
= Adapter Pattern, a directory of sites =&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Command Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Command Pattern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Introduction&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;[http://en.wikipedia.org/wiki/Command_pattern Definition:]&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command pattern is a behavioural design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
Three terms always associated with the command pattern are client, invoker and receiver. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Client&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The client instantiates the command object and provides the information required to call the method at a later time. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Invoker&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The invoker decides when the method should be called. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Receiver&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;How does it works?&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.&lt;br /&gt;
 &lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;&lt;br /&gt;
[http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html Command Real world Example ]&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parameterized with different requests. The &amp;quot;check&amp;quot; at a diner is an example of a Command pattern. The waiter or waitress takes an order, or command from a customer, and encapsulates that order by writing it on the check. The order is then queued for a short order cook. Note that the pad of &amp;quot;checks&amp;quot; used by different diners is not dependent on the menu, and therefore they can support commands to cook many different items.&lt;br /&gt;
&lt;br /&gt;
[[File:http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif]]&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;br&amp;gt;When Would I Use This Pattern?&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The Command Pattern is useful when:&amp;lt;br&amp;gt;&lt;br /&gt;
•	A history of requests is needed&amp;lt;br&amp;gt;&lt;br /&gt;
•	You need callback functionality&amp;lt;br&amp;gt;&lt;br /&gt;
•	Requests need to be handled at variant times or in variant orders&amp;lt;br&amp;gt;&lt;br /&gt;
•	The invoker should be decoupled from the object handling the invocation.&amp;lt;br&amp;gt;&lt;br /&gt;
You'll see command being used a lot when you need to have multiple undo operations, where a stack of the recently executed commands are maintained. To implement the undo, all you need to do is get the last Command in the stack and execute it's undo() method.&lt;br /&gt;
You'll also find Command useful for wizards, progress bars, GUI buttons and menu actions, and other transactional behavior. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Directory of sites&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;[http://msdn.microsoft.com/en-us/magazine/cc163920.aspx Command pattern on MSDN Microsoft Website] &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Command Pattern to -Simplify Distributed System Design Using the Command Pattern, MSMQ, and .NET &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Object-based collaboration VS service-based collaboration&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Communication or exchange of request and service and the format or the pattern based on play an important criteria in successful implementation of  object-based collaboration and service-oriented architecture. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Systems generally share a common platform, allowing you to optimize in ways you can't on widely dispersed -systems that span various operating systems and development environments are preferred to follow object-based collaboration than service-based collaboration.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
But generally the participants are hybrid and they don't share a common platform,  in such situations service oriented architecture comes handy. Here the requests are queued in Microsoft Message Queuing (MSMQ) which implements Command design pattern. &lt;br /&gt;
&lt;br /&gt;
[[File:http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif]]&lt;br /&gt;
&lt;br /&gt;
The distributed command system discussed here is the combination of reliable messaging, services, and the Command pattern. While most often seen as a design pattern for implementing desktop application menu systems, the Command pattern offers advantages for distributed systems when combined with a robust messaging infrastructure like MSMQ. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Learning: &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The concept is very simple, in service oriented architecture, the participants don't share a common platform, hence you need a mechanism, a design pattern, a module to issue requests to objects.  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The module that is doing the requesting doesn’t know what the receiver will be, or what operation will be requested.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pattern and Participants&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class	Description&lt;br /&gt;
Command	Abstract base class for all Command objects&lt;br /&gt;
Concrete command	Any number of classes derived from Command&lt;br /&gt;
Client	The creator of a command&lt;br /&gt;
Invoker	Executes a command&lt;br /&gt;
Receiver	Any class that is acted on by a command&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Another Example: &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In ASP.NET, the database communication are based on the commands, the database commands such as create connection, command statement, parameter doesn't know what the object is and what the object it is requesting and these are typical example of command design pattern it best.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;&lt;br /&gt;
Conclusion&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The Command pattern specifies a set of interactions that you can apply to the development of distributed systems in .NET. Use this pattern in your application to implement a high-performance, extendable backbone connecting your near link nodes that run .NET-enabled operating systems to achieve optimal performance and rapid development. Distributed commands are a mix of classic object orientation, messaging, and service architecture that together fill a definite need in the distributed systems space.&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture Advantages]&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefit of the Command class is that it makes it easier to add new functions to an application. &lt;br /&gt;
If we add a new capability, we no longer need to reopen the Controller class to code routing information. &lt;br /&gt;
Instead, we encapsulate the information in a Command object and assign it to whatever control we use in the View to invoke the new capability. &lt;br /&gt;
When the Command gets passed to the Controller, the Controller can use it to route the request, without knowing anything about it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;br&amp;gt;Disadvantages:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.phpwact.org/ Command Pattern on Web Application Component Toolkit (WACT)]&lt;br /&gt;
&lt;br /&gt;
[http://www.phpwact.org/pattern/command Web Application Component Toolkit]&lt;br /&gt;
The Web Application Component Toolkit is a framework for creating web applications. WACT facilitates a modular approach where individual, independent or reusable components may be integrated into a larger web application. WACT assists in implementing the Model View Controller pattern and the related Domain Model, Template View, Front Controller and Application Controller patterns. WACT emphasizes writing secure web applications.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
WACT sees Command Pattern as&lt;br /&gt;
A GoF Design Pattern with the intent: “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”&lt;br /&gt;
&lt;br /&gt;
In WACT, this is seen with the FormController, where users can define a class which must contain the method performAction();&lt;br /&gt;
&lt;br /&gt;
class AddressForm extends PostFormController {&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
 &lt;br /&gt;
    function InitializeActions() {&lt;br /&gt;
        $this-&amp;gt;registerSubmitAction(&amp;quot;register&amp;quot;, &amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
        $this-&amp;gt;registerDefaultSubmitAction(&amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The registerSubmitAction call above points the controller at the class RegisterUser, which is where the Command pattern resides.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class RegisterUser {&lt;br /&gt;
 &lt;br /&gt;
    function performAction(&amp;amp;$context) {&lt;br /&gt;
        $DataSpace =&amp;amp; $context-&amp;gt;getDataSpace();&lt;br /&gt;
        $Page =&amp;amp; new Template('/input/thanks.html');&lt;br /&gt;
        $Page-&amp;gt;import($DataSpace-&amp;gt;export());&lt;br /&gt;
        $Page-&amp;gt;display();&lt;br /&gt;
        return FORM_COMPLETE;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The FormController expects to find the performAction method in the Action Class RegisterUser, calling it when the form is submitted (and successfully validated).&lt;br /&gt;
&lt;br /&gt;
The Command pattern is a Gang of Four Design Pattern, used to allow a function name to be wrapped up in a class namespace, so that some code, which will call the function, can be given an instance of one of many classes without caring which which instance it’s dealing with.&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
Here the Form controller in its post functionality exhibits command pattern as it requires the object that calls the post form functionality to implement performAction method, the actions that the object needs to perform when the form is submitted, but the post form controller doesn't know what are actions performed by the performAction method and whose performAction  method is called all it requires is it needs a class that has the performAction method to post it forms. &lt;br /&gt;
Hence the performAction acts like a command that is being called whenever the form is posted.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example:&lt;br /&gt;
PHP example &lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
/* interface */ class Tag {&lt;br /&gt;
   // The abstract command method&lt;br /&gt;
   function render($content);&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Bold {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;b&amp;gt;$content&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Italic {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;i&amp;gt;$content&amp;lt;/i&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
$commands = array (&lt;br /&gt;
    new Bold(),new Italic(), new Italic(), new Bold()&lt;br /&gt;
);&lt;br /&gt;
 &lt;br /&gt;
// Iterate over the command objects&lt;br /&gt;
foreach ( $commands as $command ) {&lt;br /&gt;
    echo ( $command-&amp;gt;render('Hello World!') );&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the PHP example provided above, class Bold, Italic works on the function render.&lt;br /&gt;
&lt;br /&gt;
In class Tag, all it does is for each command in the command object, render functionality is called, the render doesn't know which command, bold or italics is called and what doesn't knows the functionality of command (bold or italics) &lt;br /&gt;
&lt;br /&gt;
Applications&lt;br /&gt;
&lt;br /&gt;
The Command pattern is used widely on in web based application frameworks like Java Struts from a PHP Perspective, usually in Action Classes, to allow framework users to define logic which responds to “events” in the application. Action Classes can be regarded as the “verbs” of the framework - they act on things - they do things.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Comparison to similar patterns&lt;br /&gt;
&lt;br /&gt;
Commands and Mementos have some similarity due to the fact they both work with an object's internal properties. The Command pattern keeps a record of changes to an object's state, and applies those changes in an ad-hoc fashion. A Memento also records changes to an object's state, and can restore that state at any time. The Chain of Responsibility pattern seems to handle processing in a similar manner to the Command, except it hands off processing to another process linearly unlike the Command pattern. An Interpreter pattern works in the example above because we are using language elements to determine which changes to apply at a given time.&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
Since the object that implements the command design doesn't know what function it does and what object it affects, as it acts like a black box, the implementation of the objects that is calling the black box can be changed and it can be implemented independently without affecting the flow of the program.&lt;br /&gt;
&lt;br /&gt;
Hence command pattern gives a wide range of flexibility and sophisticated design features.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
It complicates the application design. Every pattern complicates application design; they’re used because the benefits outweigh the cost. In fact, they should only be used when the benefits outweigh the costs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Javaworld&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
&lt;br /&gt;
According to Javaworld,&lt;br /&gt;
&lt;br /&gt;
The design patterns not only accelerate the design phase of an object-oriented (OO) project but also increase the productivity of the development team and quality of the software. &lt;br /&gt;
&lt;br /&gt;
They describe a Command pattern as an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. &lt;br /&gt;
&lt;br /&gt;
Sender -  invokes an operation&lt;br /&gt;
Receiver - receives the request to execute a certain operation. &lt;br /&gt;
Decoupling - the sender has no knowledge of the Receiver's interface&lt;br /&gt;
Request  - the command that is to be executed. &lt;br /&gt;
&lt;br /&gt;
Command pattern provides us flexibility as well as extensibility.&lt;br /&gt;
&lt;br /&gt;
The program discussed in the javaworld web directory is class Fan {&lt;br /&gt;
        public void startRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void stopRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is not rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Light {&lt;br /&gt;
        public void turnOn( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is on &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void turnOff( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is off&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Switch {&lt;br /&gt;
        private Command UpCommand, DownCommand;&lt;br /&gt;
        public Switch( Command Up, Command Down) {&lt;br /&gt;
                UpCommand = Up; // concrete Command registers itself with the invoker &lt;br /&gt;
                DownCommand = Down;&lt;br /&gt;
        }&lt;br /&gt;
        void flipUp( ) { // invoker calls back concrete Command, which executes the Command on the receiver &lt;br /&gt;
                        UpCommand . execute ( ) ;                           &lt;br /&gt;
        }&lt;br /&gt;
        void flipDown( ) {&lt;br /&gt;
                        DownCommand . execute ( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOnCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOnCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOn( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOffCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOffCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOff( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOnCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOnCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . startRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOffCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOffCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . stopRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
public class TestCommand {&lt;br /&gt;
                public static void main(String[] args) {&lt;br /&gt;
                        Light  testLight = new Light( );&lt;br /&gt;
                        LightOnCommand testLOC = new LightOnCommand(testLight);&lt;br /&gt;
                        LightOffCommand testLFC = new LightOffCommand(testLight);&lt;br /&gt;
                        Switch testSwitch = new Switch( testLOC,testLFC);       &lt;br /&gt;
                        testSwitch.flipUp( );&lt;br /&gt;
                        testSwitch.flipDown( );&lt;br /&gt;
                        Fan testFan = new Fan( );&lt;br /&gt;
                        FanOnCommand foc = new FanOnCommand(testFan);&lt;br /&gt;
                        FanOffCommand ffc = new FanOffCommand(testFan);&lt;br /&gt;
                        Switch ts = new Switch( foc,ffc);&lt;br /&gt;
                        ts.flipUp( );&lt;br /&gt;
                        ts.flipDown( ); &lt;br /&gt;
                }&lt;br /&gt;
}               &lt;br /&gt;
Command.java&lt;br /&gt;
public interface Command {&lt;br /&gt;
        public abstract void execute ( );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the above example,&lt;br /&gt;
The class Fan has startRotate() and stopRotate() methods,the class Light has turnOn() and turnOff() methods.&lt;br /&gt;
The catch here is startRotate of Fan and turnOn of Light is performed when the switch is turned on and stopRotate of Fan and turnOff of Light is performed when the switch in turned off.&lt;br /&gt;
&lt;br /&gt;
Keeping this real world catch, the class switch is created as a kind of interface for &lt;br /&gt;
fan and light to work on it turn on and turn off requests.&lt;br /&gt;
&lt;br /&gt;
The switch has flipUp and flipDown methods which acts like command design implementation.&lt;br /&gt;
The switch doesn't know what action it performs on flipUp and flipDown and what object is affected when flipUp and flipDown is pressed. &lt;br /&gt;
&lt;br /&gt;
Hence flipUp and flipDown acts like a command without knowing what function it does and what object it affects, but eventually performing the required action. That is the beauty of command design. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Command pattern has the following advantages:&lt;br /&gt;
1.	A command decouples the object that invokes the operation from the one that knows how to perform it.&lt;br /&gt;
2.	A command is a first-class object. It can be manipulated and extended like any other object.&lt;br /&gt;
3.	Commands can be assembled into a composite command.&lt;br /&gt;
4.	It's easy to add new commands, because you don't have to change the existing classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References&lt;br /&gt;
http://www.itsmedia.ca/the-command-pattern-and-restful-interfaces/&lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Pateximg14.gif&amp;diff=70578</id>
		<title>File:Pateximg14.gif</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Pateximg14.gif&amp;diff=70578"/>
		<updated>2012-11-19T09:48:20Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70577</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w45 pg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70577"/>
		<updated>2012-11-19T09:47:18Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Command Pattern =&lt;br /&gt;
&lt;br /&gt;
= Adapter Pattern, a directory of sites =&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Command Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Command Pattern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Introduction&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;[http://en.wikipedia.org/wiki/Command_pattern Definition:]&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command pattern is a behavioural design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
Three terms always associated with the command pattern are client, invoker and receiver. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Client&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The client instantiates the command object and provides the information required to call the method at a later time. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Invoker&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The invoker decides when the method should be called. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Receiver&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;How does it works?&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.&lt;br /&gt;
 &lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;&lt;br /&gt;
[http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html Command Real world Example ]&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parameterized with different requests. The &amp;quot;check&amp;quot; at a diner is an example of a Command pattern. The waiter or waitress takes an order, or command from a customer, and encapsulates that order by writing it on the check. The order is then queued for a short order cook. Note that the pad of &amp;quot;checks&amp;quot; used by different diners is not dependent on the menu, and therefore they can support commands to cook many different items.&lt;br /&gt;
 &lt;br /&gt;
[[File:http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;br&amp;gt;When Would I Use This Pattern?&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The Command Pattern is useful when:&amp;lt;br&amp;gt;&lt;br /&gt;
•	A history of requests is needed&amp;lt;br&amp;gt;&lt;br /&gt;
•	You need callback functionality&amp;lt;br&amp;gt;&lt;br /&gt;
•	Requests need to be handled at variant times or in variant orders&amp;lt;br&amp;gt;&lt;br /&gt;
•	The invoker should be decoupled from the object handling the invocation.&amp;lt;br&amp;gt;&lt;br /&gt;
You'll see command being used a lot when you need to have multiple undo operations, where a stack of the recently executed commands are maintained. To implement the undo, all you need to do is get the last Command in the stack and execute it's undo() method.&lt;br /&gt;
You'll also find Command useful for wizards, progress bars, GUI buttons and menu actions, and other transactional behavior. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Directory of sites&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;[http://msdn.microsoft.com/en-us/magazine/cc163920.aspx Command pattern on MSDN Microsoft Website] &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Command Pattern to -Simplify Distributed System Design Using the Command Pattern, MSMQ, and .NET &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Object-based collaboration VS service-based collaboration&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Communication or exchange of request and service and the format or the pattern based on play an important criteria in successful implementation of  object-based collaboration and service-oriented architecture. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Systems generally share a common platform, allowing you to optimize in ways you can't on widely dispersed -systems that span various operating systems and development environments are preferred to follow object-based collaboration than service-based collaboration.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
But generally the participants are hybrid and they don't share a common platform,  in such situations service oriented architecture comes handy. Here the requests are queued in Microsoft Message Queuing (MSMQ) which implements Command design pattern. &lt;br /&gt;
&lt;br /&gt;
[[File:http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif]]&lt;br /&gt;
&lt;br /&gt;
The distributed command system discussed here is the combination of reliable messaging, services, and the Command pattern. While most often seen as a design pattern for implementing desktop application menu systems, the Command pattern offers advantages for distributed systems when combined with a robust messaging infrastructure like MSMQ. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Learning: &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The concept is very simple, in service oriented architecture, the participants don't share a common platform, hence you need a mechanism, a design pattern, a module to issue requests to objects.  &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The module that is doing the requesting doesn’t know what the receiver will be, or what operation will be requested.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pattern and Participants&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class	Description&lt;br /&gt;
Command	Abstract base class for all Command objects&lt;br /&gt;
Concrete command	Any number of classes derived from Command&lt;br /&gt;
Client	The creator of a command&lt;br /&gt;
Invoker	Executes a command&lt;br /&gt;
Receiver	Any class that is acted on by a command&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Another Example: &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
In ASP.NET, the database communication are based on the commands, the database commands such as create connection, command statement, parameter doesn't know what the object is and what the object it is requesting and these are typical example of command design pattern it best.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;&lt;br /&gt;
Conclusion&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The Command pattern specifies a set of interactions that you can apply to the development of distributed systems in .NET. Use this pattern in your application to implement a high-performance, extendable backbone connecting your near link nodes that run .NET-enabled operating systems to achieve optimal performance and rapid development. Distributed commands are a mix of classic object orientation, messaging, and service architecture that together fill a definite need in the distributed systems space.&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture Advantages]&amp;lt;/b&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The benefit of the Command class is that it makes it easier to add new functions to an application. &lt;br /&gt;
If we add a new capability, we no longer need to reopen the Controller class to code routing information. &lt;br /&gt;
Instead, we encapsulate the information in a Command object and assign it to whatever control we use in the View to invoke the new capability. &lt;br /&gt;
When the Command gets passed to the Controller, the Controller can use it to route the request, without knowing anything about it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;&amp;lt;br&amp;gt;Disadvantages:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.phpwact.org/ Command Pattern on Web Application Component Toolkit (WACT)]&lt;br /&gt;
&lt;br /&gt;
[http://www.phpwact.org/pattern/command Web Application Component Toolkit]&lt;br /&gt;
The Web Application Component Toolkit is a framework for creating web applications. WACT facilitates a modular approach where individual, independent or reusable components may be integrated into a larger web application. WACT assists in implementing the Model View Controller pattern and the related Domain Model, Template View, Front Controller and Application Controller patterns. WACT emphasizes writing secure web applications.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
WACT sees Command Pattern as&lt;br /&gt;
A GoF Design Pattern with the intent: “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”&lt;br /&gt;
&lt;br /&gt;
In WACT, this is seen with the FormController, where users can define a class which must contain the method performAction();&lt;br /&gt;
&lt;br /&gt;
class AddressForm extends PostFormController {&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
 &lt;br /&gt;
    function InitializeActions() {&lt;br /&gt;
        $this-&amp;gt;registerSubmitAction(&amp;quot;register&amp;quot;, &amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
        $this-&amp;gt;registerDefaultSubmitAction(&amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The registerSubmitAction call above points the controller at the class RegisterUser, which is where the Command pattern resides.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class RegisterUser {&lt;br /&gt;
 &lt;br /&gt;
    function performAction(&amp;amp;$context) {&lt;br /&gt;
        $DataSpace =&amp;amp; $context-&amp;gt;getDataSpace();&lt;br /&gt;
        $Page =&amp;amp; new Template('/input/thanks.html');&lt;br /&gt;
        $Page-&amp;gt;import($DataSpace-&amp;gt;export());&lt;br /&gt;
        $Page-&amp;gt;display();&lt;br /&gt;
        return FORM_COMPLETE;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The FormController expects to find the performAction method in the Action Class RegisterUser, calling it when the form is submitted (and successfully validated).&lt;br /&gt;
&lt;br /&gt;
The Command pattern is a Gang of Four Design Pattern, used to allow a function name to be wrapped up in a class namespace, so that some code, which will call the function, can be given an instance of one of many classes without caring which which instance it’s dealing with.&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
Here the Form controller in its post functionality exhibits command pattern as it requires the object that calls the post form functionality to implement performAction method, the actions that the object needs to perform when the form is submitted, but the post form controller doesn't know what are actions performed by the performAction method and whose performAction  method is called all it requires is it needs a class that has the performAction method to post it forms. &lt;br /&gt;
Hence the performAction acts like a command that is being called whenever the form is posted.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example:&lt;br /&gt;
PHP example &lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
/* interface */ class Tag {&lt;br /&gt;
   // The abstract command method&lt;br /&gt;
   function render($content);&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Bold {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;b&amp;gt;$content&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Italic {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;i&amp;gt;$content&amp;lt;/i&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
$commands = array (&lt;br /&gt;
    new Bold(),new Italic(), new Italic(), new Bold()&lt;br /&gt;
);&lt;br /&gt;
 &lt;br /&gt;
// Iterate over the command objects&lt;br /&gt;
foreach ( $commands as $command ) {&lt;br /&gt;
    echo ( $command-&amp;gt;render('Hello World!') );&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the PHP example provided above, class Bold, Italic works on the function render.&lt;br /&gt;
&lt;br /&gt;
In class Tag, all it does is for each command in the command object, render functionality is called, the render doesn't know which command, bold or italics is called and what doesn't knows the functionality of command (bold or italics) &lt;br /&gt;
&lt;br /&gt;
Applications&lt;br /&gt;
&lt;br /&gt;
The Command pattern is used widely on in web based application frameworks like Java Struts from a PHP Perspective, usually in Action Classes, to allow framework users to define logic which responds to “events” in the application. Action Classes can be regarded as the “verbs” of the framework - they act on things - they do things.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Comparison to similar patterns&lt;br /&gt;
&lt;br /&gt;
Commands and Mementos have some similarity due to the fact they both work with an object's internal properties. The Command pattern keeps a record of changes to an object's state, and applies those changes in an ad-hoc fashion. A Memento also records changes to an object's state, and can restore that state at any time. The Chain of Responsibility pattern seems to handle processing in a similar manner to the Command, except it hands off processing to another process linearly unlike the Command pattern. An Interpreter pattern works in the example above because we are using language elements to determine which changes to apply at a given time.&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
Since the object that implements the command design doesn't know what function it does and what object it affects, as it acts like a black box, the implementation of the objects that is calling the black box can be changed and it can be implemented independently without affecting the flow of the program.&lt;br /&gt;
&lt;br /&gt;
Hence command pattern gives a wide range of flexibility and sophisticated design features.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
It complicates the application design. Every pattern complicates application design; they’re used because the benefits outweigh the cost. In fact, they should only be used when the benefits outweigh the costs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Javaworld&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
&lt;br /&gt;
According to Javaworld,&lt;br /&gt;
&lt;br /&gt;
The design patterns not only accelerate the design phase of an object-oriented (OO) project but also increase the productivity of the development team and quality of the software. &lt;br /&gt;
&lt;br /&gt;
They describe a Command pattern as an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. &lt;br /&gt;
&lt;br /&gt;
Sender -  invokes an operation&lt;br /&gt;
Receiver - receives the request to execute a certain operation. &lt;br /&gt;
Decoupling - the sender has no knowledge of the Receiver's interface&lt;br /&gt;
Request  - the command that is to be executed. &lt;br /&gt;
&lt;br /&gt;
Command pattern provides us flexibility as well as extensibility.&lt;br /&gt;
&lt;br /&gt;
The program discussed in the javaworld web directory is class Fan {&lt;br /&gt;
        public void startRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void stopRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is not rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Light {&lt;br /&gt;
        public void turnOn( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is on &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void turnOff( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is off&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Switch {&lt;br /&gt;
        private Command UpCommand, DownCommand;&lt;br /&gt;
        public Switch( Command Up, Command Down) {&lt;br /&gt;
                UpCommand = Up; // concrete Command registers itself with the invoker &lt;br /&gt;
                DownCommand = Down;&lt;br /&gt;
        }&lt;br /&gt;
        void flipUp( ) { // invoker calls back concrete Command, which executes the Command on the receiver &lt;br /&gt;
                        UpCommand . execute ( ) ;                           &lt;br /&gt;
        }&lt;br /&gt;
        void flipDown( ) {&lt;br /&gt;
                        DownCommand . execute ( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOnCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOnCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOn( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOffCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOffCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOff( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOnCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOnCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . startRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOffCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOffCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . stopRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
public class TestCommand {&lt;br /&gt;
                public static void main(String[] args) {&lt;br /&gt;
                        Light  testLight = new Light( );&lt;br /&gt;
                        LightOnCommand testLOC = new LightOnCommand(testLight);&lt;br /&gt;
                        LightOffCommand testLFC = new LightOffCommand(testLight);&lt;br /&gt;
                        Switch testSwitch = new Switch( testLOC,testLFC);       &lt;br /&gt;
                        testSwitch.flipUp( );&lt;br /&gt;
                        testSwitch.flipDown( );&lt;br /&gt;
                        Fan testFan = new Fan( );&lt;br /&gt;
                        FanOnCommand foc = new FanOnCommand(testFan);&lt;br /&gt;
                        FanOffCommand ffc = new FanOffCommand(testFan);&lt;br /&gt;
                        Switch ts = new Switch( foc,ffc);&lt;br /&gt;
                        ts.flipUp( );&lt;br /&gt;
                        ts.flipDown( ); &lt;br /&gt;
                }&lt;br /&gt;
}               &lt;br /&gt;
Command.java&lt;br /&gt;
public interface Command {&lt;br /&gt;
        public abstract void execute ( );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the above example,&lt;br /&gt;
The class Fan has startRotate() and stopRotate() methods,the class Light has turnOn() and turnOff() methods.&lt;br /&gt;
The catch here is startRotate of Fan and turnOn of Light is performed when the switch is turned on and stopRotate of Fan and turnOff of Light is performed when the switch in turned off.&lt;br /&gt;
&lt;br /&gt;
Keeping this real world catch, the class switch is created as a kind of interface for &lt;br /&gt;
fan and light to work on it turn on and turn off requests.&lt;br /&gt;
&lt;br /&gt;
The switch has flipUp and flipDown methods which acts like command design implementation.&lt;br /&gt;
The switch doesn't know what action it performs on flipUp and flipDown and what object is affected when flipUp and flipDown is pressed. &lt;br /&gt;
&lt;br /&gt;
Hence flipUp and flipDown acts like a command without knowing what function it does and what object it affects, but eventually performing the required action. That is the beauty of command design. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Command pattern has the following advantages:&lt;br /&gt;
1.	A command decouples the object that invokes the operation from the one that knows how to perform it.&lt;br /&gt;
2.	A command is a first-class object. It can be manipulated and extended like any other object.&lt;br /&gt;
3.	Commands can be assembled into a composite command.&lt;br /&gt;
4.	It's easy to add new commands, because you don't have to change the existing classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References&lt;br /&gt;
http://www.itsmedia.ca/the-command-pattern-and-restful-interfaces/&lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70576</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w45 pg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70576"/>
		<updated>2012-11-19T09:39:19Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Command Pattern =&lt;br /&gt;
&lt;br /&gt;
= Adapter Pattern, a directory of sites =&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Command Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Command Pattern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Introduction&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;[http://en.wikipedia.org/wiki/Command_pattern Definition:]&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command pattern is a behavioural design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
Three terms always associated with the command pattern are client, invoker and receiver. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Client&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The client instantiates the command object and provides the information required to call the method at a later time. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Invoker&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The invoker decides when the method should be called. &lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;b&amp;gt;Receiver&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
How does it works?&lt;br /&gt;
Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.&lt;br /&gt;
 &lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
&lt;br /&gt;
Command Real world Example &lt;br /&gt;
(http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html)&lt;br /&gt;
&lt;br /&gt;
The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parameterized with different requests. The &amp;quot;check&amp;quot; at a diner is an example of a Command pattern. The waiter or waitress takes an order, or command from a customer, and encapsulates that order by writing it on the check. The order is then queued for a short order cook. Note that the pad of &amp;quot;checks&amp;quot; used by different diners is not dependent on the menu, and therefore they can support commands to cook many different items.&lt;br /&gt;
 &lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;br /&gt;
&lt;br /&gt;
When Would I Use This Pattern?&lt;br /&gt;
The Command Pattern is useful when:&lt;br /&gt;
•	A history of requests is needed&lt;br /&gt;
•	You need callback functionality&lt;br /&gt;
•	Requests need to be handled at variant times or in variant orders&lt;br /&gt;
•	The invoker should be decoupled from the object handling the invocation.&lt;br /&gt;
You'll see command being used a lot when you need to have multiple undo operations, where a stack of the recently executed commands are maintained. To implement the undo, all you need to do is get the last Command in the stack and execute it's undo() method.&lt;br /&gt;
You'll also find Command useful for wizards, progress bars, GUI buttons and menu actions, and other transactional behaviour. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Directory of sites&lt;br /&gt;
http://msdn.microsoft.com/en-us/magazine/cc163920.aspx&lt;br /&gt;
&lt;br /&gt;
Command pattern on MSDN Microsoft Website:&lt;br /&gt;
Command Pattern to -Simplify Distributed System Design Using the Command Pattern, MSMQ, and .NET &lt;br /&gt;
&lt;br /&gt;
Object-based collaboration VS service-based collaboration&lt;br /&gt;
Communication or exchange of request and service and the format or the pattern based on play an important criteria in successful implementation of  object-based collaboration and service-oriented architecture. &lt;br /&gt;
&lt;br /&gt;
Systems generally share a common platform, allowing you to optimize in ways you can't on widely dispersed -systems that span various operating systems and development environments are preferred to follow object-based collaboration than service-based collaboration.&lt;br /&gt;
&lt;br /&gt;
But generally the participants are hybrid and they don't share a common platform,  in such situations service oriented architecture comes handy. here the requests are queued in Microsoft Message Queuing (MSMQ) which implements Command design pattern. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The distributed command system discussed here is the combination of reliable messaging, services, and the Command pattern. While most often seen as a design pattern for implementing desktop application menu systems, the Command pattern offers advantages for distributed systems when combined with a robust messaging infrastructure like MSMQ. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Learning: &lt;br /&gt;
&lt;br /&gt;
The concept is very simple, in service oriented architecture, the participants don't share a common platform, hence you need a mechanism, a design pattern, a module to issue requests to objects.  &lt;br /&gt;
&lt;br /&gt;
The module that is doing the requesting doesn’t know what the receiver will be, or what operation will be requested.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Pattern and Participants&lt;br /&gt;
Class	Description&lt;br /&gt;
Command	Abstract base class for all Command objects&lt;br /&gt;
Concrete command	Any number of classes derived from Command&lt;br /&gt;
Client	The creator of a command&lt;br /&gt;
Invoker	Executes a command&lt;br /&gt;
Receiver	Any class that is acted on by a command&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example: &lt;br /&gt;
In ASP.NET, the database communication are based on the commands, the database commands such as create connection, command statement, parameter doesn't know what the object is and what the object it is requesting and these are typical example of command design pattern it best.&lt;br /&gt;
&lt;br /&gt;
Conclusion:&lt;br /&gt;
The Command pattern specifies a set of interactions that you can apply to the development of distributed systems in .NET. Use this pattern in your application to implement a high-performance, extendable backbone connecting your near link nodes that run .NET-enabled operating systems to achieve optimal performance and rapid development. Distributed commands are a mix of classic object orientation, messaging, and service architecture that together fill a definite need in the distributed systems space.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
The benefit of the Command class is that it makes it easier to add new functions to an application. &lt;br /&gt;
If we add a new capability, we no longer need to reopen the Controller class to code routing information. &lt;br /&gt;
Instead, we encapsulate the information in a Command object and assign it to whatever control we use in the View to invoke the new capability. &lt;br /&gt;
When the Command gets passed to the Controller, the Controller can use it to route the request, without knowing anything about it.&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. &lt;br /&gt;
&lt;br /&gt;
The routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. &lt;br /&gt;
&lt;br /&gt;
The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Web Application Component Toolkit (WACT)&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
&lt;br /&gt;
Web Application Component Toolkit&lt;br /&gt;
The Web Application Component Toolkit is a framework for creating web applications. WACT facilitates a modular approach where individual, independent or reusable components may be integrated into a larger web application. WACT assists in implementing the Model View Controller pattern and the related Domain Model, Template View, Front Controller and Application Controller patterns. WACT emphasizes writing secure web applications.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
WACT sees Command Pattern as&lt;br /&gt;
A GoF Design Pattern with the intent: “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”&lt;br /&gt;
&lt;br /&gt;
In WACT, this is seen with the FormController, where users can define a class which must contain the method performAction();&lt;br /&gt;
&lt;br /&gt;
class AddressForm extends PostFormController {&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
 &lt;br /&gt;
    function InitializeActions() {&lt;br /&gt;
        $this-&amp;gt;registerSubmitAction(&amp;quot;register&amp;quot;, &amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
        $this-&amp;gt;registerDefaultSubmitAction(&amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The registerSubmitAction call above points the controller at the class RegisterUser, which is where the Command pattern resides.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class RegisterUser {&lt;br /&gt;
 &lt;br /&gt;
    function performAction(&amp;amp;$context) {&lt;br /&gt;
        $DataSpace =&amp;amp; $context-&amp;gt;getDataSpace();&lt;br /&gt;
        $Page =&amp;amp; new Template('/input/thanks.html');&lt;br /&gt;
        $Page-&amp;gt;import($DataSpace-&amp;gt;export());&lt;br /&gt;
        $Page-&amp;gt;display();&lt;br /&gt;
        return FORM_COMPLETE;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The FormController expects to find the performAction method in the Action Class RegisterUser, calling it when the form is submitted (and successfully validated).&lt;br /&gt;
&lt;br /&gt;
The Command pattern is a Gang of Four Design Pattern, used to allow a function name to be wrapped up in a class namespace, so that some code, which will call the function, can be given an instance of one of many classes without caring which which instance it’s dealing with.&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
Here the Form controller in its post functionality exhibits command pattern as it requires the object that calls the post form functionality to implement performAction method, the actions that the object needs to perform when the form is submitted, but the post form controller doesn't know what are actions performed by the performAction method and whose performAction  method is called all it requires is it needs a class that has the performAction method to post it forms. &lt;br /&gt;
Hence the performAction acts like a command that is being called whenever the form is posted.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example:&lt;br /&gt;
PHP example &lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
/* interface */ class Tag {&lt;br /&gt;
   // The abstract command method&lt;br /&gt;
   function render($content);&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Bold {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;b&amp;gt;$content&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Italic {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;i&amp;gt;$content&amp;lt;/i&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
$commands = array (&lt;br /&gt;
    new Bold(),new Italic(), new Italic(), new Bold()&lt;br /&gt;
);&lt;br /&gt;
 &lt;br /&gt;
// Iterate over the command objects&lt;br /&gt;
foreach ( $commands as $command ) {&lt;br /&gt;
    echo ( $command-&amp;gt;render('Hello World!') );&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the PHP example provided above, class Bold, Italic works on the function render.&lt;br /&gt;
&lt;br /&gt;
In class Tag, all it does is for each command in the command object, render functionality is called, the render doesn't know which command, bold or italics is called and what doesn't knows the functionality of command (bold or italics) &lt;br /&gt;
&lt;br /&gt;
Applications&lt;br /&gt;
&lt;br /&gt;
The Command pattern is used widely on in web based application frameworks like Java Struts from a PHP Perspective, usually in Action Classes, to allow framework users to define logic which responds to “events” in the application. Action Classes can be regarded as the “verbs” of the framework - they act on things - they do things.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Comparison to similar patterns&lt;br /&gt;
&lt;br /&gt;
Commands and Mementos have some similarity due to the fact they both work with an object's internal properties. The Command pattern keeps a record of changes to an object's state, and applies those changes in an ad-hoc fashion. A Memento also records changes to an object's state, and can restore that state at any time. The Chain of Responsibility pattern seems to handle processing in a similar manner to the Command, except it hands off processing to another process linearly unlike the Command pattern. An Interpreter pattern works in the example above because we are using language elements to determine which changes to apply at a given time.&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
Since the object that implements the command design doesn't know what function it does and what object it affects, as it acts like a black box, the implementation of the objects that is calling the black box can be changed and it can be implemented independently without affecting the flow of the program.&lt;br /&gt;
&lt;br /&gt;
Hence command pattern gives a wide range of flexibility and sophisticated design features.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
It complicates the application design. Every pattern complicates application design; they’re used because the benefits outweigh the cost. In fact, they should only be used when the benefits outweigh the costs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Javaworld&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
&lt;br /&gt;
According to Javaworld,&lt;br /&gt;
&lt;br /&gt;
The design patterns not only accelerate the design phase of an object-oriented (OO) project but also increase the productivity of the development team and quality of the software. &lt;br /&gt;
&lt;br /&gt;
They describe a Command pattern as an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. &lt;br /&gt;
&lt;br /&gt;
Sender -  invokes an operation&lt;br /&gt;
Receiver - receives the request to execute a certain operation. &lt;br /&gt;
Decoupling - the sender has no knowledge of the Receiver's interface&lt;br /&gt;
Request  - the command that is to be executed. &lt;br /&gt;
&lt;br /&gt;
Command pattern provides us flexibility as well as extensibility.&lt;br /&gt;
&lt;br /&gt;
The program discussed in the javaworld web directory is class Fan {&lt;br /&gt;
        public void startRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void stopRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is not rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Light {&lt;br /&gt;
        public void turnOn( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is on &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void turnOff( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is off&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Switch {&lt;br /&gt;
        private Command UpCommand, DownCommand;&lt;br /&gt;
        public Switch( Command Up, Command Down) {&lt;br /&gt;
                UpCommand = Up; // concrete Command registers itself with the invoker &lt;br /&gt;
                DownCommand = Down;&lt;br /&gt;
        }&lt;br /&gt;
        void flipUp( ) { // invoker calls back concrete Command, which executes the Command on the receiver &lt;br /&gt;
                        UpCommand . execute ( ) ;                           &lt;br /&gt;
        }&lt;br /&gt;
        void flipDown( ) {&lt;br /&gt;
                        DownCommand . execute ( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOnCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOnCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOn( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOffCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOffCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOff( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOnCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOnCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . startRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOffCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOffCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . stopRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
public class TestCommand {&lt;br /&gt;
                public static void main(String[] args) {&lt;br /&gt;
                        Light  testLight = new Light( );&lt;br /&gt;
                        LightOnCommand testLOC = new LightOnCommand(testLight);&lt;br /&gt;
                        LightOffCommand testLFC = new LightOffCommand(testLight);&lt;br /&gt;
                        Switch testSwitch = new Switch( testLOC,testLFC);       &lt;br /&gt;
                        testSwitch.flipUp( );&lt;br /&gt;
                        testSwitch.flipDown( );&lt;br /&gt;
                        Fan testFan = new Fan( );&lt;br /&gt;
                        FanOnCommand foc = new FanOnCommand(testFan);&lt;br /&gt;
                        FanOffCommand ffc = new FanOffCommand(testFan);&lt;br /&gt;
                        Switch ts = new Switch( foc,ffc);&lt;br /&gt;
                        ts.flipUp( );&lt;br /&gt;
                        ts.flipDown( ); &lt;br /&gt;
                }&lt;br /&gt;
}               &lt;br /&gt;
Command.java&lt;br /&gt;
public interface Command {&lt;br /&gt;
        public abstract void execute ( );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the above example,&lt;br /&gt;
The class Fan has startRotate() and stopRotate() methods,the class Light has turnOn() and turnOff() methods.&lt;br /&gt;
The catch here is startRotate of Fan and turnOn of Light is performed when the switch is turned on and stopRotate of Fan and turnOff of Light is performed when the switch in turned off.&lt;br /&gt;
&lt;br /&gt;
Keeping this real world catch, the class switch is created as a kind of interface for &lt;br /&gt;
fan and light to work on it turn on and turn off requests.&lt;br /&gt;
&lt;br /&gt;
The switch has flipUp and flipDown methods which acts like command design implementation.&lt;br /&gt;
The switch doesn't know what action it performs on flipUp and flipDown and what object is affected when flipUp and flipDown is pressed. &lt;br /&gt;
&lt;br /&gt;
Hence flipUp and flipDown acts like a command without knowing what function it does and what object it affects, but eventually performing the required action. That is the beauty of command design. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Command pattern has the following advantages:&lt;br /&gt;
1.	A command decouples the object that invokes the operation from the one that knows how to perform it.&lt;br /&gt;
2.	A command is a first-class object. It can be manipulated and extended like any other object.&lt;br /&gt;
3.	Commands can be assembled into a composite command.&lt;br /&gt;
4.	It's easy to add new commands, because you don't have to change the existing classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References&lt;br /&gt;
http://www.itsmedia.ca/the-command-pattern-and-restful-interfaces/&lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70575</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w45 pg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70575"/>
		<updated>2012-11-19T09:38:49Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Command Pattern =&lt;br /&gt;
&lt;br /&gt;
= Adapter Pattern, a directory of sites =&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Command Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Command Pattern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Introduction&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;[http://en.wikipedia.org/wiki/Command_pattern Definition:]&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command pattern is a behavioural design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
Three terms always associated with the command pattern are client, invoker and receiver. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Client&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The client instantiates the command object and provides the information required to call the method at a later time. &lt;br /&gt;
&amp;lt;b&amp;gt;Invoker&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The invoker decides when the method should be called. &lt;br /&gt;
&amp;lt;b&amp;gt;Receiver&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
How does it works?&lt;br /&gt;
Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.&lt;br /&gt;
 &lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
&lt;br /&gt;
Command Real world Example &lt;br /&gt;
(http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html)&lt;br /&gt;
&lt;br /&gt;
The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parameterized with different requests. The &amp;quot;check&amp;quot; at a diner is an example of a Command pattern. The waiter or waitress takes an order, or command from a customer, and encapsulates that order by writing it on the check. The order is then queued for a short order cook. Note that the pad of &amp;quot;checks&amp;quot; used by different diners is not dependent on the menu, and therefore they can support commands to cook many different items.&lt;br /&gt;
 &lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;br /&gt;
&lt;br /&gt;
When Would I Use This Pattern?&lt;br /&gt;
The Command Pattern is useful when:&lt;br /&gt;
•	A history of requests is needed&lt;br /&gt;
•	You need callback functionality&lt;br /&gt;
•	Requests need to be handled at variant times or in variant orders&lt;br /&gt;
•	The invoker should be decoupled from the object handling the invocation.&lt;br /&gt;
You'll see command being used a lot when you need to have multiple undo operations, where a stack of the recently executed commands are maintained. To implement the undo, all you need to do is get the last Command in the stack and execute it's undo() method.&lt;br /&gt;
You'll also find Command useful for wizards, progress bars, GUI buttons and menu actions, and other transactional behaviour. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Directory of sites&lt;br /&gt;
http://msdn.microsoft.com/en-us/magazine/cc163920.aspx&lt;br /&gt;
&lt;br /&gt;
Command pattern on MSDN Microsoft Website:&lt;br /&gt;
Command Pattern to -Simplify Distributed System Design Using the Command Pattern, MSMQ, and .NET &lt;br /&gt;
&lt;br /&gt;
Object-based collaboration VS service-based collaboration&lt;br /&gt;
Communication or exchange of request and service and the format or the pattern based on play an important criteria in successful implementation of  object-based collaboration and service-oriented architecture. &lt;br /&gt;
&lt;br /&gt;
Systems generally share a common platform, allowing you to optimize in ways you can't on widely dispersed -systems that span various operating systems and development environments are preferred to follow object-based collaboration than service-based collaboration.&lt;br /&gt;
&lt;br /&gt;
But generally the participants are hybrid and they don't share a common platform,  in such situations service oriented architecture comes handy. here the requests are queued in Microsoft Message Queuing (MSMQ) which implements Command design pattern. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The distributed command system discussed here is the combination of reliable messaging, services, and the Command pattern. While most often seen as a design pattern for implementing desktop application menu systems, the Command pattern offers advantages for distributed systems when combined with a robust messaging infrastructure like MSMQ. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Learning: &lt;br /&gt;
&lt;br /&gt;
The concept is very simple, in service oriented architecture, the participants don't share a common platform, hence you need a mechanism, a design pattern, a module to issue requests to objects.  &lt;br /&gt;
&lt;br /&gt;
The module that is doing the requesting doesn’t know what the receiver will be, or what operation will be requested.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Pattern and Participants&lt;br /&gt;
Class	Description&lt;br /&gt;
Command	Abstract base class for all Command objects&lt;br /&gt;
Concrete command	Any number of classes derived from Command&lt;br /&gt;
Client	The creator of a command&lt;br /&gt;
Invoker	Executes a command&lt;br /&gt;
Receiver	Any class that is acted on by a command&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example: &lt;br /&gt;
In ASP.NET, the database communication are based on the commands, the database commands such as create connection, command statement, parameter doesn't know what the object is and what the object it is requesting and these are typical example of command design pattern it best.&lt;br /&gt;
&lt;br /&gt;
Conclusion:&lt;br /&gt;
The Command pattern specifies a set of interactions that you can apply to the development of distributed systems in .NET. Use this pattern in your application to implement a high-performance, extendable backbone connecting your near link nodes that run .NET-enabled operating systems to achieve optimal performance and rapid development. Distributed commands are a mix of classic object orientation, messaging, and service architecture that together fill a definite need in the distributed systems space.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
The benefit of the Command class is that it makes it easier to add new functions to an application. &lt;br /&gt;
If we add a new capability, we no longer need to reopen the Controller class to code routing information. &lt;br /&gt;
Instead, we encapsulate the information in a Command object and assign it to whatever control we use in the View to invoke the new capability. &lt;br /&gt;
When the Command gets passed to the Controller, the Controller can use it to route the request, without knowing anything about it.&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. &lt;br /&gt;
&lt;br /&gt;
The routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. &lt;br /&gt;
&lt;br /&gt;
The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Web Application Component Toolkit (WACT)&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
&lt;br /&gt;
Web Application Component Toolkit&lt;br /&gt;
The Web Application Component Toolkit is a framework for creating web applications. WACT facilitates a modular approach where individual, independent or reusable components may be integrated into a larger web application. WACT assists in implementing the Model View Controller pattern and the related Domain Model, Template View, Front Controller and Application Controller patterns. WACT emphasizes writing secure web applications.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
WACT sees Command Pattern as&lt;br /&gt;
A GoF Design Pattern with the intent: “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”&lt;br /&gt;
&lt;br /&gt;
In WACT, this is seen with the FormController, where users can define a class which must contain the method performAction();&lt;br /&gt;
&lt;br /&gt;
class AddressForm extends PostFormController {&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
 &lt;br /&gt;
    function InitializeActions() {&lt;br /&gt;
        $this-&amp;gt;registerSubmitAction(&amp;quot;register&amp;quot;, &amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
        $this-&amp;gt;registerDefaultSubmitAction(&amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The registerSubmitAction call above points the controller at the class RegisterUser, which is where the Command pattern resides.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class RegisterUser {&lt;br /&gt;
 &lt;br /&gt;
    function performAction(&amp;amp;$context) {&lt;br /&gt;
        $DataSpace =&amp;amp; $context-&amp;gt;getDataSpace();&lt;br /&gt;
        $Page =&amp;amp; new Template('/input/thanks.html');&lt;br /&gt;
        $Page-&amp;gt;import($DataSpace-&amp;gt;export());&lt;br /&gt;
        $Page-&amp;gt;display();&lt;br /&gt;
        return FORM_COMPLETE;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The FormController expects to find the performAction method in the Action Class RegisterUser, calling it when the form is submitted (and successfully validated).&lt;br /&gt;
&lt;br /&gt;
The Command pattern is a Gang of Four Design Pattern, used to allow a function name to be wrapped up in a class namespace, so that some code, which will call the function, can be given an instance of one of many classes without caring which which instance it’s dealing with.&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
Here the Form controller in its post functionality exhibits command pattern as it requires the object that calls the post form functionality to implement performAction method, the actions that the object needs to perform when the form is submitted, but the post form controller doesn't know what are actions performed by the performAction method and whose performAction  method is called all it requires is it needs a class that has the performAction method to post it forms. &lt;br /&gt;
Hence the performAction acts like a command that is being called whenever the form is posted.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example:&lt;br /&gt;
PHP example &lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
/* interface */ class Tag {&lt;br /&gt;
   // The abstract command method&lt;br /&gt;
   function render($content);&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Bold {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;b&amp;gt;$content&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Italic {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;i&amp;gt;$content&amp;lt;/i&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
$commands = array (&lt;br /&gt;
    new Bold(),new Italic(), new Italic(), new Bold()&lt;br /&gt;
);&lt;br /&gt;
 &lt;br /&gt;
// Iterate over the command objects&lt;br /&gt;
foreach ( $commands as $command ) {&lt;br /&gt;
    echo ( $command-&amp;gt;render('Hello World!') );&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the PHP example provided above, class Bold, Italic works on the function render.&lt;br /&gt;
&lt;br /&gt;
In class Tag, all it does is for each command in the command object, render functionality is called, the render doesn't know which command, bold or italics is called and what doesn't knows the functionality of command (bold or italics) &lt;br /&gt;
&lt;br /&gt;
Applications&lt;br /&gt;
&lt;br /&gt;
The Command pattern is used widely on in web based application frameworks like Java Struts from a PHP Perspective, usually in Action Classes, to allow framework users to define logic which responds to “events” in the application. Action Classes can be regarded as the “verbs” of the framework - they act on things - they do things.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Comparison to similar patterns&lt;br /&gt;
&lt;br /&gt;
Commands and Mementos have some similarity due to the fact they both work with an object's internal properties. The Command pattern keeps a record of changes to an object's state, and applies those changes in an ad-hoc fashion. A Memento also records changes to an object's state, and can restore that state at any time. The Chain of Responsibility pattern seems to handle processing in a similar manner to the Command, except it hands off processing to another process linearly unlike the Command pattern. An Interpreter pattern works in the example above because we are using language elements to determine which changes to apply at a given time.&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
Since the object that implements the command design doesn't know what function it does and what object it affects, as it acts like a black box, the implementation of the objects that is calling the black box can be changed and it can be implemented independently without affecting the flow of the program.&lt;br /&gt;
&lt;br /&gt;
Hence command pattern gives a wide range of flexibility and sophisticated design features.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
It complicates the application design. Every pattern complicates application design; they’re used because the benefits outweigh the cost. In fact, they should only be used when the benefits outweigh the costs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Javaworld&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
&lt;br /&gt;
According to Javaworld,&lt;br /&gt;
&lt;br /&gt;
The design patterns not only accelerate the design phase of an object-oriented (OO) project but also increase the productivity of the development team and quality of the software. &lt;br /&gt;
&lt;br /&gt;
They describe a Command pattern as an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. &lt;br /&gt;
&lt;br /&gt;
Sender -  invokes an operation&lt;br /&gt;
Receiver - receives the request to execute a certain operation. &lt;br /&gt;
Decoupling - the sender has no knowledge of the Receiver's interface&lt;br /&gt;
Request  - the command that is to be executed. &lt;br /&gt;
&lt;br /&gt;
Command pattern provides us flexibility as well as extensibility.&lt;br /&gt;
&lt;br /&gt;
The program discussed in the javaworld web directory is class Fan {&lt;br /&gt;
        public void startRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void stopRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is not rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Light {&lt;br /&gt;
        public void turnOn( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is on &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void turnOff( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is off&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Switch {&lt;br /&gt;
        private Command UpCommand, DownCommand;&lt;br /&gt;
        public Switch( Command Up, Command Down) {&lt;br /&gt;
                UpCommand = Up; // concrete Command registers itself with the invoker &lt;br /&gt;
                DownCommand = Down;&lt;br /&gt;
        }&lt;br /&gt;
        void flipUp( ) { // invoker calls back concrete Command, which executes the Command on the receiver &lt;br /&gt;
                        UpCommand . execute ( ) ;                           &lt;br /&gt;
        }&lt;br /&gt;
        void flipDown( ) {&lt;br /&gt;
                        DownCommand . execute ( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOnCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOnCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOn( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOffCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOffCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOff( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOnCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOnCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . startRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOffCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOffCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . stopRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
public class TestCommand {&lt;br /&gt;
                public static void main(String[] args) {&lt;br /&gt;
                        Light  testLight = new Light( );&lt;br /&gt;
                        LightOnCommand testLOC = new LightOnCommand(testLight);&lt;br /&gt;
                        LightOffCommand testLFC = new LightOffCommand(testLight);&lt;br /&gt;
                        Switch testSwitch = new Switch( testLOC,testLFC);       &lt;br /&gt;
                        testSwitch.flipUp( );&lt;br /&gt;
                        testSwitch.flipDown( );&lt;br /&gt;
                        Fan testFan = new Fan( );&lt;br /&gt;
                        FanOnCommand foc = new FanOnCommand(testFan);&lt;br /&gt;
                        FanOffCommand ffc = new FanOffCommand(testFan);&lt;br /&gt;
                        Switch ts = new Switch( foc,ffc);&lt;br /&gt;
                        ts.flipUp( );&lt;br /&gt;
                        ts.flipDown( ); &lt;br /&gt;
                }&lt;br /&gt;
}               &lt;br /&gt;
Command.java&lt;br /&gt;
public interface Command {&lt;br /&gt;
        public abstract void execute ( );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the above example,&lt;br /&gt;
The class Fan has startRotate() and stopRotate() methods,the class Light has turnOn() and turnOff() methods.&lt;br /&gt;
The catch here is startRotate of Fan and turnOn of Light is performed when the switch is turned on and stopRotate of Fan and turnOff of Light is performed when the switch in turned off.&lt;br /&gt;
&lt;br /&gt;
Keeping this real world catch, the class switch is created as a kind of interface for &lt;br /&gt;
fan and light to work on it turn on and turn off requests.&lt;br /&gt;
&lt;br /&gt;
The switch has flipUp and flipDown methods which acts like command design implementation.&lt;br /&gt;
The switch doesn't know what action it performs on flipUp and flipDown and what object is affected when flipUp and flipDown is pressed. &lt;br /&gt;
&lt;br /&gt;
Hence flipUp and flipDown acts like a command without knowing what function it does and what object it affects, but eventually performing the required action. That is the beauty of command design. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Command pattern has the following advantages:&lt;br /&gt;
1.	A command decouples the object that invokes the operation from the one that knows how to perform it.&lt;br /&gt;
2.	A command is a first-class object. It can be manipulated and extended like any other object.&lt;br /&gt;
3.	Commands can be assembled into a composite command.&lt;br /&gt;
4.	It's easy to add new commands, because you don't have to change the existing classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References&lt;br /&gt;
http://www.itsmedia.ca/the-command-pattern-and-restful-interfaces/&lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70574</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w45 pg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70574"/>
		<updated>2012-11-19T09:38:26Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Command Pattern =&lt;br /&gt;
&lt;br /&gt;
= Adapter Pattern, a directory of sites =&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Command Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Command Pattern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Introduction&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;[http://en.wikipedia.org/wiki/Command_pattern Definition:]&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The command pattern is a behavioural design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
Three terms always associated with the command pattern are client, invoker and receiver. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Client&amp;lt;/b&amp;gt;&lt;br /&gt;
The client instantiates the command object and provides the information required to call the method at a later time. &lt;br /&gt;
&amp;lt;b&amp;gt;Invoker&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The invoker decides when the method should be called. &lt;br /&gt;
&amp;lt;b&amp;gt;Receiver&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
How does it works?&lt;br /&gt;
Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.&lt;br /&gt;
 &lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
&lt;br /&gt;
Command Real world Example &lt;br /&gt;
(http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html)&lt;br /&gt;
&lt;br /&gt;
The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parameterized with different requests. The &amp;quot;check&amp;quot; at a diner is an example of a Command pattern. The waiter or waitress takes an order, or command from a customer, and encapsulates that order by writing it on the check. The order is then queued for a short order cook. Note that the pad of &amp;quot;checks&amp;quot; used by different diners is not dependent on the menu, and therefore they can support commands to cook many different items.&lt;br /&gt;
 &lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;br /&gt;
&lt;br /&gt;
When Would I Use This Pattern?&lt;br /&gt;
The Command Pattern is useful when:&lt;br /&gt;
•	A history of requests is needed&lt;br /&gt;
•	You need callback functionality&lt;br /&gt;
•	Requests need to be handled at variant times or in variant orders&lt;br /&gt;
•	The invoker should be decoupled from the object handling the invocation.&lt;br /&gt;
You'll see command being used a lot when you need to have multiple undo operations, where a stack of the recently executed commands are maintained. To implement the undo, all you need to do is get the last Command in the stack and execute it's undo() method.&lt;br /&gt;
You'll also find Command useful for wizards, progress bars, GUI buttons and menu actions, and other transactional behaviour. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Directory of sites&lt;br /&gt;
http://msdn.microsoft.com/en-us/magazine/cc163920.aspx&lt;br /&gt;
&lt;br /&gt;
Command pattern on MSDN Microsoft Website:&lt;br /&gt;
Command Pattern to -Simplify Distributed System Design Using the Command Pattern, MSMQ, and .NET &lt;br /&gt;
&lt;br /&gt;
Object-based collaboration VS service-based collaboration&lt;br /&gt;
Communication or exchange of request and service and the format or the pattern based on play an important criteria in successful implementation of  object-based collaboration and service-oriented architecture. &lt;br /&gt;
&lt;br /&gt;
Systems generally share a common platform, allowing you to optimize in ways you can't on widely dispersed -systems that span various operating systems and development environments are preferred to follow object-based collaboration than service-based collaboration.&lt;br /&gt;
&lt;br /&gt;
But generally the participants are hybrid and they don't share a common platform,  in such situations service oriented architecture comes handy. here the requests are queued in Microsoft Message Queuing (MSMQ) which implements Command design pattern. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The distributed command system discussed here is the combination of reliable messaging, services, and the Command pattern. While most often seen as a design pattern for implementing desktop application menu systems, the Command pattern offers advantages for distributed systems when combined with a robust messaging infrastructure like MSMQ. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Learning: &lt;br /&gt;
&lt;br /&gt;
The concept is very simple, in service oriented architecture, the participants don't share a common platform, hence you need a mechanism, a design pattern, a module to issue requests to objects.  &lt;br /&gt;
&lt;br /&gt;
The module that is doing the requesting doesn’t know what the receiver will be, or what operation will be requested.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Pattern and Participants&lt;br /&gt;
Class	Description&lt;br /&gt;
Command	Abstract base class for all Command objects&lt;br /&gt;
Concrete command	Any number of classes derived from Command&lt;br /&gt;
Client	The creator of a command&lt;br /&gt;
Invoker	Executes a command&lt;br /&gt;
Receiver	Any class that is acted on by a command&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example: &lt;br /&gt;
In ASP.NET, the database communication are based on the commands, the database commands such as create connection, command statement, parameter doesn't know what the object is and what the object it is requesting and these are typical example of command design pattern it best.&lt;br /&gt;
&lt;br /&gt;
Conclusion:&lt;br /&gt;
The Command pattern specifies a set of interactions that you can apply to the development of distributed systems in .NET. Use this pattern in your application to implement a high-performance, extendable backbone connecting your near link nodes that run .NET-enabled operating systems to achieve optimal performance and rapid development. Distributed commands are a mix of classic object orientation, messaging, and service architecture that together fill a definite need in the distributed systems space.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
The benefit of the Command class is that it makes it easier to add new functions to an application. &lt;br /&gt;
If we add a new capability, we no longer need to reopen the Controller class to code routing information. &lt;br /&gt;
Instead, we encapsulate the information in a Command object and assign it to whatever control we use in the View to invoke the new capability. &lt;br /&gt;
When the Command gets passed to the Controller, the Controller can use it to route the request, without knowing anything about it.&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. &lt;br /&gt;
&lt;br /&gt;
The routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. &lt;br /&gt;
&lt;br /&gt;
The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Web Application Component Toolkit (WACT)&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
&lt;br /&gt;
Web Application Component Toolkit&lt;br /&gt;
The Web Application Component Toolkit is a framework for creating web applications. WACT facilitates a modular approach where individual, independent or reusable components may be integrated into a larger web application. WACT assists in implementing the Model View Controller pattern and the related Domain Model, Template View, Front Controller and Application Controller patterns. WACT emphasizes writing secure web applications.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
WACT sees Command Pattern as&lt;br /&gt;
A GoF Design Pattern with the intent: “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”&lt;br /&gt;
&lt;br /&gt;
In WACT, this is seen with the FormController, where users can define a class which must contain the method performAction();&lt;br /&gt;
&lt;br /&gt;
class AddressForm extends PostFormController {&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
 &lt;br /&gt;
    function InitializeActions() {&lt;br /&gt;
        $this-&amp;gt;registerSubmitAction(&amp;quot;register&amp;quot;, &amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
        $this-&amp;gt;registerDefaultSubmitAction(&amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The registerSubmitAction call above points the controller at the class RegisterUser, which is where the Command pattern resides.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class RegisterUser {&lt;br /&gt;
 &lt;br /&gt;
    function performAction(&amp;amp;$context) {&lt;br /&gt;
        $DataSpace =&amp;amp; $context-&amp;gt;getDataSpace();&lt;br /&gt;
        $Page =&amp;amp; new Template('/input/thanks.html');&lt;br /&gt;
        $Page-&amp;gt;import($DataSpace-&amp;gt;export());&lt;br /&gt;
        $Page-&amp;gt;display();&lt;br /&gt;
        return FORM_COMPLETE;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The FormController expects to find the performAction method in the Action Class RegisterUser, calling it when the form is submitted (and successfully validated).&lt;br /&gt;
&lt;br /&gt;
The Command pattern is a Gang of Four Design Pattern, used to allow a function name to be wrapped up in a class namespace, so that some code, which will call the function, can be given an instance of one of many classes without caring which which instance it’s dealing with.&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
Here the Form controller in its post functionality exhibits command pattern as it requires the object that calls the post form functionality to implement performAction method, the actions that the object needs to perform when the form is submitted, but the post form controller doesn't know what are actions performed by the performAction method and whose performAction  method is called all it requires is it needs a class that has the performAction method to post it forms. &lt;br /&gt;
Hence the performAction acts like a command that is being called whenever the form is posted.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example:&lt;br /&gt;
PHP example &lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
/* interface */ class Tag {&lt;br /&gt;
   // The abstract command method&lt;br /&gt;
   function render($content);&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Bold {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;b&amp;gt;$content&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Italic {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;i&amp;gt;$content&amp;lt;/i&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
$commands = array (&lt;br /&gt;
    new Bold(),new Italic(), new Italic(), new Bold()&lt;br /&gt;
);&lt;br /&gt;
 &lt;br /&gt;
// Iterate over the command objects&lt;br /&gt;
foreach ( $commands as $command ) {&lt;br /&gt;
    echo ( $command-&amp;gt;render('Hello World!') );&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the PHP example provided above, class Bold, Italic works on the function render.&lt;br /&gt;
&lt;br /&gt;
In class Tag, all it does is for each command in the command object, render functionality is called, the render doesn't know which command, bold or italics is called and what doesn't knows the functionality of command (bold or italics) &lt;br /&gt;
&lt;br /&gt;
Applications&lt;br /&gt;
&lt;br /&gt;
The Command pattern is used widely on in web based application frameworks like Java Struts from a PHP Perspective, usually in Action Classes, to allow framework users to define logic which responds to “events” in the application. Action Classes can be regarded as the “verbs” of the framework - they act on things - they do things.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Comparison to similar patterns&lt;br /&gt;
&lt;br /&gt;
Commands and Mementos have some similarity due to the fact they both work with an object's internal properties. The Command pattern keeps a record of changes to an object's state, and applies those changes in an ad-hoc fashion. A Memento also records changes to an object's state, and can restore that state at any time. The Chain of Responsibility pattern seems to handle processing in a similar manner to the Command, except it hands off processing to another process linearly unlike the Command pattern. An Interpreter pattern works in the example above because we are using language elements to determine which changes to apply at a given time.&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
Since the object that implements the command design doesn't know what function it does and what object it affects, as it acts like a black box, the implementation of the objects that is calling the black box can be changed and it can be implemented independently without affecting the flow of the program.&lt;br /&gt;
&lt;br /&gt;
Hence command pattern gives a wide range of flexibility and sophisticated design features.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
It complicates the application design. Every pattern complicates application design; they’re used because the benefits outweigh the cost. In fact, they should only be used when the benefits outweigh the costs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Javaworld&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
&lt;br /&gt;
According to Javaworld,&lt;br /&gt;
&lt;br /&gt;
The design patterns not only accelerate the design phase of an object-oriented (OO) project but also increase the productivity of the development team and quality of the software. &lt;br /&gt;
&lt;br /&gt;
They describe a Command pattern as an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. &lt;br /&gt;
&lt;br /&gt;
Sender -  invokes an operation&lt;br /&gt;
Receiver - receives the request to execute a certain operation. &lt;br /&gt;
Decoupling - the sender has no knowledge of the Receiver's interface&lt;br /&gt;
Request  - the command that is to be executed. &lt;br /&gt;
&lt;br /&gt;
Command pattern provides us flexibility as well as extensibility.&lt;br /&gt;
&lt;br /&gt;
The program discussed in the javaworld web directory is class Fan {&lt;br /&gt;
        public void startRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void stopRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is not rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Light {&lt;br /&gt;
        public void turnOn( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is on &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void turnOff( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is off&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Switch {&lt;br /&gt;
        private Command UpCommand, DownCommand;&lt;br /&gt;
        public Switch( Command Up, Command Down) {&lt;br /&gt;
                UpCommand = Up; // concrete Command registers itself with the invoker &lt;br /&gt;
                DownCommand = Down;&lt;br /&gt;
        }&lt;br /&gt;
        void flipUp( ) { // invoker calls back concrete Command, which executes the Command on the receiver &lt;br /&gt;
                        UpCommand . execute ( ) ;                           &lt;br /&gt;
        }&lt;br /&gt;
        void flipDown( ) {&lt;br /&gt;
                        DownCommand . execute ( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOnCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOnCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOn( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOffCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOffCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOff( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOnCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOnCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . startRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOffCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOffCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . stopRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
public class TestCommand {&lt;br /&gt;
                public static void main(String[] args) {&lt;br /&gt;
                        Light  testLight = new Light( );&lt;br /&gt;
                        LightOnCommand testLOC = new LightOnCommand(testLight);&lt;br /&gt;
                        LightOffCommand testLFC = new LightOffCommand(testLight);&lt;br /&gt;
                        Switch testSwitch = new Switch( testLOC,testLFC);       &lt;br /&gt;
                        testSwitch.flipUp( );&lt;br /&gt;
                        testSwitch.flipDown( );&lt;br /&gt;
                        Fan testFan = new Fan( );&lt;br /&gt;
                        FanOnCommand foc = new FanOnCommand(testFan);&lt;br /&gt;
                        FanOffCommand ffc = new FanOffCommand(testFan);&lt;br /&gt;
                        Switch ts = new Switch( foc,ffc);&lt;br /&gt;
                        ts.flipUp( );&lt;br /&gt;
                        ts.flipDown( ); &lt;br /&gt;
                }&lt;br /&gt;
}               &lt;br /&gt;
Command.java&lt;br /&gt;
public interface Command {&lt;br /&gt;
        public abstract void execute ( );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the above example,&lt;br /&gt;
The class Fan has startRotate() and stopRotate() methods,the class Light has turnOn() and turnOff() methods.&lt;br /&gt;
The catch here is startRotate of Fan and turnOn of Light is performed when the switch is turned on and stopRotate of Fan and turnOff of Light is performed when the switch in turned off.&lt;br /&gt;
&lt;br /&gt;
Keeping this real world catch, the class switch is created as a kind of interface for &lt;br /&gt;
fan and light to work on it turn on and turn off requests.&lt;br /&gt;
&lt;br /&gt;
The switch has flipUp and flipDown methods which acts like command design implementation.&lt;br /&gt;
The switch doesn't know what action it performs on flipUp and flipDown and what object is affected when flipUp and flipDown is pressed. &lt;br /&gt;
&lt;br /&gt;
Hence flipUp and flipDown acts like a command without knowing what function it does and what object it affects, but eventually performing the required action. That is the beauty of command design. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Command pattern has the following advantages:&lt;br /&gt;
1.	A command decouples the object that invokes the operation from the one that knows how to perform it.&lt;br /&gt;
2.	A command is a first-class object. It can be manipulated and extended like any other object.&lt;br /&gt;
3.	Commands can be assembled into a composite command.&lt;br /&gt;
4.	It's easy to add new commands, because you don't have to change the existing classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References&lt;br /&gt;
http://www.itsmedia.ca/the-command-pattern-and-restful-interfaces/&lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70573</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w45 pg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70573"/>
		<updated>2012-11-19T09:37:33Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Command Pattern =&lt;br /&gt;
&lt;br /&gt;
= Adapter Pattern, a directory of sites =&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Command Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Command Pattern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Introduction&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Definition:]&lt;br /&gt;
&lt;br /&gt;
The command pattern is a behavioural design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
Three terms always associated with the command pattern are client, invoker and receiver. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Client&amp;lt;/b&amp;gt;&lt;br /&gt;
The client instantiates the command object and provides the information required to call the method at a later time. &lt;br /&gt;
Invoker&lt;br /&gt;
The invoker decides when the method should be called. &lt;br /&gt;
Receiver &lt;br /&gt;
The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
How does it works?&lt;br /&gt;
Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.&lt;br /&gt;
 &lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
&lt;br /&gt;
Command Real world Example &lt;br /&gt;
(http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html)&lt;br /&gt;
&lt;br /&gt;
The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parameterized with different requests. The &amp;quot;check&amp;quot; at a diner is an example of a Command pattern. The waiter or waitress takes an order, or command from a customer, and encapsulates that order by writing it on the check. The order is then queued for a short order cook. Note that the pad of &amp;quot;checks&amp;quot; used by different diners is not dependent on the menu, and therefore they can support commands to cook many different items.&lt;br /&gt;
 &lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;br /&gt;
&lt;br /&gt;
When Would I Use This Pattern?&lt;br /&gt;
The Command Pattern is useful when:&lt;br /&gt;
•	A history of requests is needed&lt;br /&gt;
•	You need callback functionality&lt;br /&gt;
•	Requests need to be handled at variant times or in variant orders&lt;br /&gt;
•	The invoker should be decoupled from the object handling the invocation.&lt;br /&gt;
You'll see command being used a lot when you need to have multiple undo operations, where a stack of the recently executed commands are maintained. To implement the undo, all you need to do is get the last Command in the stack and execute it's undo() method.&lt;br /&gt;
You'll also find Command useful for wizards, progress bars, GUI buttons and menu actions, and other transactional behaviour. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Directory of sites&lt;br /&gt;
http://msdn.microsoft.com/en-us/magazine/cc163920.aspx&lt;br /&gt;
&lt;br /&gt;
Command pattern on MSDN Microsoft Website:&lt;br /&gt;
Command Pattern to -Simplify Distributed System Design Using the Command Pattern, MSMQ, and .NET &lt;br /&gt;
&lt;br /&gt;
Object-based collaboration VS service-based collaboration&lt;br /&gt;
Communication or exchange of request and service and the format or the pattern based on play an important criteria in successful implementation of  object-based collaboration and service-oriented architecture. &lt;br /&gt;
&lt;br /&gt;
Systems generally share a common platform, allowing you to optimize in ways you can't on widely dispersed -systems that span various operating systems and development environments are preferred to follow object-based collaboration than service-based collaboration.&lt;br /&gt;
&lt;br /&gt;
But generally the participants are hybrid and they don't share a common platform,  in such situations service oriented architecture comes handy. here the requests are queued in Microsoft Message Queuing (MSMQ) which implements Command design pattern. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The distributed command system discussed here is the combination of reliable messaging, services, and the Command pattern. While most often seen as a design pattern for implementing desktop application menu systems, the Command pattern offers advantages for distributed systems when combined with a robust messaging infrastructure like MSMQ. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Learning: &lt;br /&gt;
&lt;br /&gt;
The concept is very simple, in service oriented architecture, the participants don't share a common platform, hence you need a mechanism, a design pattern, a module to issue requests to objects.  &lt;br /&gt;
&lt;br /&gt;
The module that is doing the requesting doesn’t know what the receiver will be, or what operation will be requested.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Pattern and Participants&lt;br /&gt;
Class	Description&lt;br /&gt;
Command	Abstract base class for all Command objects&lt;br /&gt;
Concrete command	Any number of classes derived from Command&lt;br /&gt;
Client	The creator of a command&lt;br /&gt;
Invoker	Executes a command&lt;br /&gt;
Receiver	Any class that is acted on by a command&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example: &lt;br /&gt;
In ASP.NET, the database communication are based on the commands, the database commands such as create connection, command statement, parameter doesn't know what the object is and what the object it is requesting and these are typical example of command design pattern it best.&lt;br /&gt;
&lt;br /&gt;
Conclusion:&lt;br /&gt;
The Command pattern specifies a set of interactions that you can apply to the development of distributed systems in .NET. Use this pattern in your application to implement a high-performance, extendable backbone connecting your near link nodes that run .NET-enabled operating systems to achieve optimal performance and rapid development. Distributed commands are a mix of classic object orientation, messaging, and service architecture that together fill a definite need in the distributed systems space.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
The benefit of the Command class is that it makes it easier to add new functions to an application. &lt;br /&gt;
If we add a new capability, we no longer need to reopen the Controller class to code routing information. &lt;br /&gt;
Instead, we encapsulate the information in a Command object and assign it to whatever control we use in the View to invoke the new capability. &lt;br /&gt;
When the Command gets passed to the Controller, the Controller can use it to route the request, without knowing anything about it.&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. &lt;br /&gt;
&lt;br /&gt;
The routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. &lt;br /&gt;
&lt;br /&gt;
The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Web Application Component Toolkit (WACT)&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
&lt;br /&gt;
Web Application Component Toolkit&lt;br /&gt;
The Web Application Component Toolkit is a framework for creating web applications. WACT facilitates a modular approach where individual, independent or reusable components may be integrated into a larger web application. WACT assists in implementing the Model View Controller pattern and the related Domain Model, Template View, Front Controller and Application Controller patterns. WACT emphasizes writing secure web applications.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
WACT sees Command Pattern as&lt;br /&gt;
A GoF Design Pattern with the intent: “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”&lt;br /&gt;
&lt;br /&gt;
In WACT, this is seen with the FormController, where users can define a class which must contain the method performAction();&lt;br /&gt;
&lt;br /&gt;
class AddressForm extends PostFormController {&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
 &lt;br /&gt;
    function InitializeActions() {&lt;br /&gt;
        $this-&amp;gt;registerSubmitAction(&amp;quot;register&amp;quot;, &amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
        $this-&amp;gt;registerDefaultSubmitAction(&amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The registerSubmitAction call above points the controller at the class RegisterUser, which is where the Command pattern resides.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class RegisterUser {&lt;br /&gt;
 &lt;br /&gt;
    function performAction(&amp;amp;$context) {&lt;br /&gt;
        $DataSpace =&amp;amp; $context-&amp;gt;getDataSpace();&lt;br /&gt;
        $Page =&amp;amp; new Template('/input/thanks.html');&lt;br /&gt;
        $Page-&amp;gt;import($DataSpace-&amp;gt;export());&lt;br /&gt;
        $Page-&amp;gt;display();&lt;br /&gt;
        return FORM_COMPLETE;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The FormController expects to find the performAction method in the Action Class RegisterUser, calling it when the form is submitted (and successfully validated).&lt;br /&gt;
&lt;br /&gt;
The Command pattern is a Gang of Four Design Pattern, used to allow a function name to be wrapped up in a class namespace, so that some code, which will call the function, can be given an instance of one of many classes without caring which which instance it’s dealing with.&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
Here the Form controller in its post functionality exhibits command pattern as it requires the object that calls the post form functionality to implement performAction method, the actions that the object needs to perform when the form is submitted, but the post form controller doesn't know what are actions performed by the performAction method and whose performAction  method is called all it requires is it needs a class that has the performAction method to post it forms. &lt;br /&gt;
Hence the performAction acts like a command that is being called whenever the form is posted.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example:&lt;br /&gt;
PHP example &lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
/* interface */ class Tag {&lt;br /&gt;
   // The abstract command method&lt;br /&gt;
   function render($content);&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Bold {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;b&amp;gt;$content&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Italic {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;i&amp;gt;$content&amp;lt;/i&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
$commands = array (&lt;br /&gt;
    new Bold(),new Italic(), new Italic(), new Bold()&lt;br /&gt;
);&lt;br /&gt;
 &lt;br /&gt;
// Iterate over the command objects&lt;br /&gt;
foreach ( $commands as $command ) {&lt;br /&gt;
    echo ( $command-&amp;gt;render('Hello World!') );&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the PHP example provided above, class Bold, Italic works on the function render.&lt;br /&gt;
&lt;br /&gt;
In class Tag, all it does is for each command in the command object, render functionality is called, the render doesn't know which command, bold or italics is called and what doesn't knows the functionality of command (bold or italics) &lt;br /&gt;
&lt;br /&gt;
Applications&lt;br /&gt;
&lt;br /&gt;
The Command pattern is used widely on in web based application frameworks like Java Struts from a PHP Perspective, usually in Action Classes, to allow framework users to define logic which responds to “events” in the application. Action Classes can be regarded as the “verbs” of the framework - they act on things - they do things.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Comparison to similar patterns&lt;br /&gt;
&lt;br /&gt;
Commands and Mementos have some similarity due to the fact they both work with an object's internal properties. The Command pattern keeps a record of changes to an object's state, and applies those changes in an ad-hoc fashion. A Memento also records changes to an object's state, and can restore that state at any time. The Chain of Responsibility pattern seems to handle processing in a similar manner to the Command, except it hands off processing to another process linearly unlike the Command pattern. An Interpreter pattern works in the example above because we are using language elements to determine which changes to apply at a given time.&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
Since the object that implements the command design doesn't know what function it does and what object it affects, as it acts like a black box, the implementation of the objects that is calling the black box can be changed and it can be implemented independently without affecting the flow of the program.&lt;br /&gt;
&lt;br /&gt;
Hence command pattern gives a wide range of flexibility and sophisticated design features.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
It complicates the application design. Every pattern complicates application design; they’re used because the benefits outweigh the cost. In fact, they should only be used when the benefits outweigh the costs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Javaworld&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
&lt;br /&gt;
According to Javaworld,&lt;br /&gt;
&lt;br /&gt;
The design patterns not only accelerate the design phase of an object-oriented (OO) project but also increase the productivity of the development team and quality of the software. &lt;br /&gt;
&lt;br /&gt;
They describe a Command pattern as an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. &lt;br /&gt;
&lt;br /&gt;
Sender -  invokes an operation&lt;br /&gt;
Receiver - receives the request to execute a certain operation. &lt;br /&gt;
Decoupling - the sender has no knowledge of the Receiver's interface&lt;br /&gt;
Request  - the command that is to be executed. &lt;br /&gt;
&lt;br /&gt;
Command pattern provides us flexibility as well as extensibility.&lt;br /&gt;
&lt;br /&gt;
The program discussed in the javaworld web directory is class Fan {&lt;br /&gt;
        public void startRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void stopRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is not rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Light {&lt;br /&gt;
        public void turnOn( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is on &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void turnOff( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is off&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Switch {&lt;br /&gt;
        private Command UpCommand, DownCommand;&lt;br /&gt;
        public Switch( Command Up, Command Down) {&lt;br /&gt;
                UpCommand = Up; // concrete Command registers itself with the invoker &lt;br /&gt;
                DownCommand = Down;&lt;br /&gt;
        }&lt;br /&gt;
        void flipUp( ) { // invoker calls back concrete Command, which executes the Command on the receiver &lt;br /&gt;
                        UpCommand . execute ( ) ;                           &lt;br /&gt;
        }&lt;br /&gt;
        void flipDown( ) {&lt;br /&gt;
                        DownCommand . execute ( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOnCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOnCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOn( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOffCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOffCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOff( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOnCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOnCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . startRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOffCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOffCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . stopRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
public class TestCommand {&lt;br /&gt;
                public static void main(String[] args) {&lt;br /&gt;
                        Light  testLight = new Light( );&lt;br /&gt;
                        LightOnCommand testLOC = new LightOnCommand(testLight);&lt;br /&gt;
                        LightOffCommand testLFC = new LightOffCommand(testLight);&lt;br /&gt;
                        Switch testSwitch = new Switch( testLOC,testLFC);       &lt;br /&gt;
                        testSwitch.flipUp( );&lt;br /&gt;
                        testSwitch.flipDown( );&lt;br /&gt;
                        Fan testFan = new Fan( );&lt;br /&gt;
                        FanOnCommand foc = new FanOnCommand(testFan);&lt;br /&gt;
                        FanOffCommand ffc = new FanOffCommand(testFan);&lt;br /&gt;
                        Switch ts = new Switch( foc,ffc);&lt;br /&gt;
                        ts.flipUp( );&lt;br /&gt;
                        ts.flipDown( ); &lt;br /&gt;
                }&lt;br /&gt;
}               &lt;br /&gt;
Command.java&lt;br /&gt;
public interface Command {&lt;br /&gt;
        public abstract void execute ( );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the above example,&lt;br /&gt;
The class Fan has startRotate() and stopRotate() methods,the class Light has turnOn() and turnOff() methods.&lt;br /&gt;
The catch here is startRotate of Fan and turnOn of Light is performed when the switch is turned on and stopRotate of Fan and turnOff of Light is performed when the switch in turned off.&lt;br /&gt;
&lt;br /&gt;
Keeping this real world catch, the class switch is created as a kind of interface for &lt;br /&gt;
fan and light to work on it turn on and turn off requests.&lt;br /&gt;
&lt;br /&gt;
The switch has flipUp and flipDown methods which acts like command design implementation.&lt;br /&gt;
The switch doesn't know what action it performs on flipUp and flipDown and what object is affected when flipUp and flipDown is pressed. &lt;br /&gt;
&lt;br /&gt;
Hence flipUp and flipDown acts like a command without knowing what function it does and what object it affects, but eventually performing the required action. That is the beauty of command design. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Command pattern has the following advantages:&lt;br /&gt;
1.	A command decouples the object that invokes the operation from the one that knows how to perform it.&lt;br /&gt;
2.	A command is a first-class object. It can be manipulated and extended like any other object.&lt;br /&gt;
3.	Commands can be assembled into a composite command.&lt;br /&gt;
4.	It's easy to add new commands, because you don't have to change the existing classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References&lt;br /&gt;
http://www.itsmedia.ca/the-command-pattern-and-restful-interfaces/&lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70572</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w45 pg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70572"/>
		<updated>2012-11-19T09:36:52Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Command Pattern =&lt;br /&gt;
&lt;br /&gt;
= Adapter Pattern, a directory of sites =&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Command Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Command Pattern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Introduction&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definition: (http://en.wikipedia.org/wiki/Command_pattern)&lt;br /&gt;
&lt;br /&gt;
The command pattern is a behavioural design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
Three terms always associated with the command pattern are client, invoker and receiver. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Client&amp;lt;/b&amp;gt;&lt;br /&gt;
The client instantiates the command object and provides the information required to call the method at a later time. &lt;br /&gt;
Invoker&lt;br /&gt;
The invoker decides when the method should be called. &lt;br /&gt;
Receiver &lt;br /&gt;
The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
How does it works?&lt;br /&gt;
Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.&lt;br /&gt;
 &lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
&lt;br /&gt;
Command Real world Example &lt;br /&gt;
(http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html)&lt;br /&gt;
&lt;br /&gt;
The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parameterized with different requests. The &amp;quot;check&amp;quot; at a diner is an example of a Command pattern. The waiter or waitress takes an order, or command from a customer, and encapsulates that order by writing it on the check. The order is then queued for a short order cook. Note that the pad of &amp;quot;checks&amp;quot; used by different diners is not dependent on the menu, and therefore they can support commands to cook many different items.&lt;br /&gt;
 &lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;br /&gt;
&lt;br /&gt;
When Would I Use This Pattern?&lt;br /&gt;
The Command Pattern is useful when:&lt;br /&gt;
•	A history of requests is needed&lt;br /&gt;
•	You need callback functionality&lt;br /&gt;
•	Requests need to be handled at variant times or in variant orders&lt;br /&gt;
•	The invoker should be decoupled from the object handling the invocation.&lt;br /&gt;
You'll see command being used a lot when you need to have multiple undo operations, where a stack of the recently executed commands are maintained. To implement the undo, all you need to do is get the last Command in the stack and execute it's undo() method.&lt;br /&gt;
You'll also find Command useful for wizards, progress bars, GUI buttons and menu actions, and other transactional behaviour. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Directory of sites&lt;br /&gt;
http://msdn.microsoft.com/en-us/magazine/cc163920.aspx&lt;br /&gt;
&lt;br /&gt;
Command pattern on MSDN Microsoft Website:&lt;br /&gt;
Command Pattern to -Simplify Distributed System Design Using the Command Pattern, MSMQ, and .NET &lt;br /&gt;
&lt;br /&gt;
Object-based collaboration VS service-based collaboration&lt;br /&gt;
Communication or exchange of request and service and the format or the pattern based on play an important criteria in successful implementation of  object-based collaboration and service-oriented architecture. &lt;br /&gt;
&lt;br /&gt;
Systems generally share a common platform, allowing you to optimize in ways you can't on widely dispersed -systems that span various operating systems and development environments are preferred to follow object-based collaboration than service-based collaboration.&lt;br /&gt;
&lt;br /&gt;
But generally the participants are hybrid and they don't share a common platform,  in such situations service oriented architecture comes handy. here the requests are queued in Microsoft Message Queuing (MSMQ) which implements Command design pattern. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The distributed command system discussed here is the combination of reliable messaging, services, and the Command pattern. While most often seen as a design pattern for implementing desktop application menu systems, the Command pattern offers advantages for distributed systems when combined with a robust messaging infrastructure like MSMQ. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Learning: &lt;br /&gt;
&lt;br /&gt;
The concept is very simple, in service oriented architecture, the participants don't share a common platform, hence you need a mechanism, a design pattern, a module to issue requests to objects.  &lt;br /&gt;
&lt;br /&gt;
The module that is doing the requesting doesn’t know what the receiver will be, or what operation will be requested.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Pattern and Participants&lt;br /&gt;
Class	Description&lt;br /&gt;
Command	Abstract base class for all Command objects&lt;br /&gt;
Concrete command	Any number of classes derived from Command&lt;br /&gt;
Client	The creator of a command&lt;br /&gt;
Invoker	Executes a command&lt;br /&gt;
Receiver	Any class that is acted on by a command&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example: &lt;br /&gt;
In ASP.NET, the database communication are based on the commands, the database commands such as create connection, command statement, parameter doesn't know what the object is and what the object it is requesting and these are typical example of command design pattern it best.&lt;br /&gt;
&lt;br /&gt;
Conclusion:&lt;br /&gt;
The Command pattern specifies a set of interactions that you can apply to the development of distributed systems in .NET. Use this pattern in your application to implement a high-performance, extendable backbone connecting your near link nodes that run .NET-enabled operating systems to achieve optimal performance and rapid development. Distributed commands are a mix of classic object orientation, messaging, and service architecture that together fill a definite need in the distributed systems space.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
The benefit of the Command class is that it makes it easier to add new functions to an application. &lt;br /&gt;
If we add a new capability, we no longer need to reopen the Controller class to code routing information. &lt;br /&gt;
Instead, we encapsulate the information in a Command object and assign it to whatever control we use in the View to invoke the new capability. &lt;br /&gt;
When the Command gets passed to the Controller, the Controller can use it to route the request, without knowing anything about it.&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. &lt;br /&gt;
&lt;br /&gt;
The routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. &lt;br /&gt;
&lt;br /&gt;
The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Web Application Component Toolkit (WACT)&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
&lt;br /&gt;
Web Application Component Toolkit&lt;br /&gt;
The Web Application Component Toolkit is a framework for creating web applications. WACT facilitates a modular approach where individual, independent or reusable components may be integrated into a larger web application. WACT assists in implementing the Model View Controller pattern and the related Domain Model, Template View, Front Controller and Application Controller patterns. WACT emphasizes writing secure web applications.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
WACT sees Command Pattern as&lt;br /&gt;
A GoF Design Pattern with the intent: “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”&lt;br /&gt;
&lt;br /&gt;
In WACT, this is seen with the FormController, where users can define a class which must contain the method performAction();&lt;br /&gt;
&lt;br /&gt;
class AddressForm extends PostFormController {&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
 &lt;br /&gt;
    function InitializeActions() {&lt;br /&gt;
        $this-&amp;gt;registerSubmitAction(&amp;quot;register&amp;quot;, &amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
        $this-&amp;gt;registerDefaultSubmitAction(&amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The registerSubmitAction call above points the controller at the class RegisterUser, which is where the Command pattern resides.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class RegisterUser {&lt;br /&gt;
 &lt;br /&gt;
    function performAction(&amp;amp;$context) {&lt;br /&gt;
        $DataSpace =&amp;amp; $context-&amp;gt;getDataSpace();&lt;br /&gt;
        $Page =&amp;amp; new Template('/input/thanks.html');&lt;br /&gt;
        $Page-&amp;gt;import($DataSpace-&amp;gt;export());&lt;br /&gt;
        $Page-&amp;gt;display();&lt;br /&gt;
        return FORM_COMPLETE;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The FormController expects to find the performAction method in the Action Class RegisterUser, calling it when the form is submitted (and successfully validated).&lt;br /&gt;
&lt;br /&gt;
The Command pattern is a Gang of Four Design Pattern, used to allow a function name to be wrapped up in a class namespace, so that some code, which will call the function, can be given an instance of one of many classes without caring which which instance it’s dealing with.&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
Here the Form controller in its post functionality exhibits command pattern as it requires the object that calls the post form functionality to implement performAction method, the actions that the object needs to perform when the form is submitted, but the post form controller doesn't know what are actions performed by the performAction method and whose performAction  method is called all it requires is it needs a class that has the performAction method to post it forms. &lt;br /&gt;
Hence the performAction acts like a command that is being called whenever the form is posted.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example:&lt;br /&gt;
PHP example &lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
/* interface */ class Tag {&lt;br /&gt;
   // The abstract command method&lt;br /&gt;
   function render($content);&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Bold {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;b&amp;gt;$content&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Italic {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;i&amp;gt;$content&amp;lt;/i&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
$commands = array (&lt;br /&gt;
    new Bold(),new Italic(), new Italic(), new Bold()&lt;br /&gt;
);&lt;br /&gt;
 &lt;br /&gt;
// Iterate over the command objects&lt;br /&gt;
foreach ( $commands as $command ) {&lt;br /&gt;
    echo ( $command-&amp;gt;render('Hello World!') );&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the PHP example provided above, class Bold, Italic works on the function render.&lt;br /&gt;
&lt;br /&gt;
In class Tag, all it does is for each command in the command object, render functionality is called, the render doesn't know which command, bold or italics is called and what doesn't knows the functionality of command (bold or italics) &lt;br /&gt;
&lt;br /&gt;
Applications&lt;br /&gt;
&lt;br /&gt;
The Command pattern is used widely on in web based application frameworks like Java Struts from a PHP Perspective, usually in Action Classes, to allow framework users to define logic which responds to “events” in the application. Action Classes can be regarded as the “verbs” of the framework - they act on things - they do things.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Comparison to similar patterns&lt;br /&gt;
&lt;br /&gt;
Commands and Mementos have some similarity due to the fact they both work with an object's internal properties. The Command pattern keeps a record of changes to an object's state, and applies those changes in an ad-hoc fashion. A Memento also records changes to an object's state, and can restore that state at any time. The Chain of Responsibility pattern seems to handle processing in a similar manner to the Command, except it hands off processing to another process linearly unlike the Command pattern. An Interpreter pattern works in the example above because we are using language elements to determine which changes to apply at a given time.&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
Since the object that implements the command design doesn't know what function it does and what object it affects, as it acts like a black box, the implementation of the objects that is calling the black box can be changed and it can be implemented independently without affecting the flow of the program.&lt;br /&gt;
&lt;br /&gt;
Hence command pattern gives a wide range of flexibility and sophisticated design features.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
It complicates the application design. Every pattern complicates application design; they’re used because the benefits outweigh the cost. In fact, they should only be used when the benefits outweigh the costs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Javaworld&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
&lt;br /&gt;
According to Javaworld,&lt;br /&gt;
&lt;br /&gt;
The design patterns not only accelerate the design phase of an object-oriented (OO) project but also increase the productivity of the development team and quality of the software. &lt;br /&gt;
&lt;br /&gt;
They describe a Command pattern as an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. &lt;br /&gt;
&lt;br /&gt;
Sender -  invokes an operation&lt;br /&gt;
Receiver - receives the request to execute a certain operation. &lt;br /&gt;
Decoupling - the sender has no knowledge of the Receiver's interface&lt;br /&gt;
Request  - the command that is to be executed. &lt;br /&gt;
&lt;br /&gt;
Command pattern provides us flexibility as well as extensibility.&lt;br /&gt;
&lt;br /&gt;
The program discussed in the javaworld web directory is class Fan {&lt;br /&gt;
        public void startRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void stopRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is not rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Light {&lt;br /&gt;
        public void turnOn( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is on &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void turnOff( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is off&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Switch {&lt;br /&gt;
        private Command UpCommand, DownCommand;&lt;br /&gt;
        public Switch( Command Up, Command Down) {&lt;br /&gt;
                UpCommand = Up; // concrete Command registers itself with the invoker &lt;br /&gt;
                DownCommand = Down;&lt;br /&gt;
        }&lt;br /&gt;
        void flipUp( ) { // invoker calls back concrete Command, which executes the Command on the receiver &lt;br /&gt;
                        UpCommand . execute ( ) ;                           &lt;br /&gt;
        }&lt;br /&gt;
        void flipDown( ) {&lt;br /&gt;
                        DownCommand . execute ( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOnCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOnCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOn( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOffCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOffCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOff( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOnCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOnCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . startRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOffCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOffCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . stopRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
public class TestCommand {&lt;br /&gt;
                public static void main(String[] args) {&lt;br /&gt;
                        Light  testLight = new Light( );&lt;br /&gt;
                        LightOnCommand testLOC = new LightOnCommand(testLight);&lt;br /&gt;
                        LightOffCommand testLFC = new LightOffCommand(testLight);&lt;br /&gt;
                        Switch testSwitch = new Switch( testLOC,testLFC);       &lt;br /&gt;
                        testSwitch.flipUp( );&lt;br /&gt;
                        testSwitch.flipDown( );&lt;br /&gt;
                        Fan testFan = new Fan( );&lt;br /&gt;
                        FanOnCommand foc = new FanOnCommand(testFan);&lt;br /&gt;
                        FanOffCommand ffc = new FanOffCommand(testFan);&lt;br /&gt;
                        Switch ts = new Switch( foc,ffc);&lt;br /&gt;
                        ts.flipUp( );&lt;br /&gt;
                        ts.flipDown( ); &lt;br /&gt;
                }&lt;br /&gt;
}               &lt;br /&gt;
Command.java&lt;br /&gt;
public interface Command {&lt;br /&gt;
        public abstract void execute ( );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the above example,&lt;br /&gt;
The class Fan has startRotate() and stopRotate() methods,the class Light has turnOn() and turnOff() methods.&lt;br /&gt;
The catch here is startRotate of Fan and turnOn of Light is performed when the switch is turned on and stopRotate of Fan and turnOff of Light is performed when the switch in turned off.&lt;br /&gt;
&lt;br /&gt;
Keeping this real world catch, the class switch is created as a kind of interface for &lt;br /&gt;
fan and light to work on it turn on and turn off requests.&lt;br /&gt;
&lt;br /&gt;
The switch has flipUp and flipDown methods which acts like command design implementation.&lt;br /&gt;
The switch doesn't know what action it performs on flipUp and flipDown and what object is affected when flipUp and flipDown is pressed. &lt;br /&gt;
&lt;br /&gt;
Hence flipUp and flipDown acts like a command without knowing what function it does and what object it affects, but eventually performing the required action. That is the beauty of command design. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Command pattern has the following advantages:&lt;br /&gt;
1.	A command decouples the object that invokes the operation from the one that knows how to perform it.&lt;br /&gt;
2.	A command is a first-class object. It can be manipulated and extended like any other object.&lt;br /&gt;
3.	Commands can be assembled into a composite command.&lt;br /&gt;
4.	It's easy to add new commands, because you don't have to change the existing classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References&lt;br /&gt;
http://www.itsmedia.ca/the-command-pattern-and-restful-interfaces/&lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70571</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w45 pg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70571"/>
		<updated>2012-11-19T09:35:43Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Command Pattern =&lt;br /&gt;
&lt;br /&gt;
= Adapter Pattern, a directory of sites =&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Command Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Command Pattern.&lt;br /&gt;
&lt;br /&gt;
Introduction&lt;br /&gt;
&lt;br /&gt;
Definition: (http://en.wikipedia.org/wiki/Command_pattern)&lt;br /&gt;
&lt;br /&gt;
The command pattern is a behavioural design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
Three terms always associated with the command pattern are client, invoker and receiver. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Client&amp;lt;/b&amp;gt;&lt;br /&gt;
The client instantiates the command object and provides the information required to call the method at a later time. &lt;br /&gt;
Invoker&lt;br /&gt;
The invoker decides when the method should be called. &lt;br /&gt;
Receiver &lt;br /&gt;
The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
How does it works?&lt;br /&gt;
Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.&lt;br /&gt;
 &lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
&lt;br /&gt;
Command Real world Example &lt;br /&gt;
(http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html)&lt;br /&gt;
&lt;br /&gt;
The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parameterized with different requests. The &amp;quot;check&amp;quot; at a diner is an example of a Command pattern. The waiter or waitress takes an order, or command from a customer, and encapsulates that order by writing it on the check. The order is then queued for a short order cook. Note that the pad of &amp;quot;checks&amp;quot; used by different diners is not dependent on the menu, and therefore they can support commands to cook many different items.&lt;br /&gt;
 &lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;br /&gt;
&lt;br /&gt;
When Would I Use This Pattern?&lt;br /&gt;
The Command Pattern is useful when:&lt;br /&gt;
•	A history of requests is needed&lt;br /&gt;
•	You need callback functionality&lt;br /&gt;
•	Requests need to be handled at variant times or in variant orders&lt;br /&gt;
•	The invoker should be decoupled from the object handling the invocation.&lt;br /&gt;
You'll see command being used a lot when you need to have multiple undo operations, where a stack of the recently executed commands are maintained. To implement the undo, all you need to do is get the last Command in the stack and execute it's undo() method.&lt;br /&gt;
You'll also find Command useful for wizards, progress bars, GUI buttons and menu actions, and other transactional behaviour. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Directory of sites&lt;br /&gt;
http://msdn.microsoft.com/en-us/magazine/cc163920.aspx&lt;br /&gt;
&lt;br /&gt;
Command pattern on MSDN Microsoft Website:&lt;br /&gt;
Command Pattern to -Simplify Distributed System Design Using the Command Pattern, MSMQ, and .NET &lt;br /&gt;
&lt;br /&gt;
Object-based collaboration VS service-based collaboration&lt;br /&gt;
Communication or exchange of request and service and the format or the pattern based on play an important criteria in successful implementation of  object-based collaboration and service-oriented architecture. &lt;br /&gt;
&lt;br /&gt;
Systems generally share a common platform, allowing you to optimize in ways you can't on widely dispersed -systems that span various operating systems and development environments are preferred to follow object-based collaboration than service-based collaboration.&lt;br /&gt;
&lt;br /&gt;
But generally the participants are hybrid and they don't share a common platform,  in such situations service oriented architecture comes handy. here the requests are queued in Microsoft Message Queuing (MSMQ) which implements Command design pattern. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The distributed command system discussed here is the combination of reliable messaging, services, and the Command pattern. While most often seen as a design pattern for implementing desktop application menu systems, the Command pattern offers advantages for distributed systems when combined with a robust messaging infrastructure like MSMQ. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Learning: &lt;br /&gt;
&lt;br /&gt;
The concept is very simple, in service oriented architecture, the participants don't share a common platform, hence you need a mechanism, a design pattern, a module to issue requests to objects.  &lt;br /&gt;
&lt;br /&gt;
The module that is doing the requesting doesn’t know what the receiver will be, or what operation will be requested.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Pattern and Participants&lt;br /&gt;
Class	Description&lt;br /&gt;
Command	Abstract base class for all Command objects&lt;br /&gt;
Concrete command	Any number of classes derived from Command&lt;br /&gt;
Client	The creator of a command&lt;br /&gt;
Invoker	Executes a command&lt;br /&gt;
Receiver	Any class that is acted on by a command&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example: &lt;br /&gt;
In ASP.NET, the database communication are based on the commands, the database commands such as create connection, command statement, parameter doesn't know what the object is and what the object it is requesting and these are typical example of command design pattern it best.&lt;br /&gt;
&lt;br /&gt;
Conclusion:&lt;br /&gt;
The Command pattern specifies a set of interactions that you can apply to the development of distributed systems in .NET. Use this pattern in your application to implement a high-performance, extendable backbone connecting your near link nodes that run .NET-enabled operating systems to achieve optimal performance and rapid development. Distributed commands are a mix of classic object orientation, messaging, and service architecture that together fill a definite need in the distributed systems space.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
The benefit of the Command class is that it makes it easier to add new functions to an application. &lt;br /&gt;
If we add a new capability, we no longer need to reopen the Controller class to code routing information. &lt;br /&gt;
Instead, we encapsulate the information in a Command object and assign it to whatever control we use in the View to invoke the new capability. &lt;br /&gt;
When the Command gets passed to the Controller, the Controller can use it to route the request, without knowing anything about it.&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. &lt;br /&gt;
&lt;br /&gt;
The routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. &lt;br /&gt;
&lt;br /&gt;
The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Web Application Component Toolkit (WACT)&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
&lt;br /&gt;
Web Application Component Toolkit&lt;br /&gt;
The Web Application Component Toolkit is a framework for creating web applications. WACT facilitates a modular approach where individual, independent or reusable components may be integrated into a larger web application. WACT assists in implementing the Model View Controller pattern and the related Domain Model, Template View, Front Controller and Application Controller patterns. WACT emphasizes writing secure web applications.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
WACT sees Command Pattern as&lt;br /&gt;
A GoF Design Pattern with the intent: “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”&lt;br /&gt;
&lt;br /&gt;
In WACT, this is seen with the FormController, where users can define a class which must contain the method performAction();&lt;br /&gt;
&lt;br /&gt;
class AddressForm extends PostFormController {&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
 &lt;br /&gt;
    function InitializeActions() {&lt;br /&gt;
        $this-&amp;gt;registerSubmitAction(&amp;quot;register&amp;quot;, &amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
        $this-&amp;gt;registerDefaultSubmitAction(&amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The registerSubmitAction call above points the controller at the class RegisterUser, which is where the Command pattern resides.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class RegisterUser {&lt;br /&gt;
 &lt;br /&gt;
    function performAction(&amp;amp;$context) {&lt;br /&gt;
        $DataSpace =&amp;amp; $context-&amp;gt;getDataSpace();&lt;br /&gt;
        $Page =&amp;amp; new Template('/input/thanks.html');&lt;br /&gt;
        $Page-&amp;gt;import($DataSpace-&amp;gt;export());&lt;br /&gt;
        $Page-&amp;gt;display();&lt;br /&gt;
        return FORM_COMPLETE;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The FormController expects to find the performAction method in the Action Class RegisterUser, calling it when the form is submitted (and successfully validated).&lt;br /&gt;
&lt;br /&gt;
The Command pattern is a Gang of Four Design Pattern, used to allow a function name to be wrapped up in a class namespace, so that some code, which will call the function, can be given an instance of one of many classes without caring which which instance it’s dealing with.&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
Here the Form controller in its post functionality exhibits command pattern as it requires the object that calls the post form functionality to implement performAction method, the actions that the object needs to perform when the form is submitted, but the post form controller doesn't know what are actions performed by the performAction method and whose performAction  method is called all it requires is it needs a class that has the performAction method to post it forms. &lt;br /&gt;
Hence the performAction acts like a command that is being called whenever the form is posted.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example:&lt;br /&gt;
PHP example &lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
/* interface */ class Tag {&lt;br /&gt;
   // The abstract command method&lt;br /&gt;
   function render($content);&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Bold {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;b&amp;gt;$content&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Italic {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;i&amp;gt;$content&amp;lt;/i&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
$commands = array (&lt;br /&gt;
    new Bold(),new Italic(), new Italic(), new Bold()&lt;br /&gt;
);&lt;br /&gt;
 &lt;br /&gt;
// Iterate over the command objects&lt;br /&gt;
foreach ( $commands as $command ) {&lt;br /&gt;
    echo ( $command-&amp;gt;render('Hello World!') );&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the PHP example provided above, class Bold, Italic works on the function render.&lt;br /&gt;
&lt;br /&gt;
In class Tag, all it does is for each command in the command object, render functionality is called, the render doesn't know which command, bold or italics is called and what doesn't knows the functionality of command (bold or italics) &lt;br /&gt;
&lt;br /&gt;
Applications&lt;br /&gt;
&lt;br /&gt;
The Command pattern is used widely on in web based application frameworks like Java Struts from a PHP Perspective, usually in Action Classes, to allow framework users to define logic which responds to “events” in the application. Action Classes can be regarded as the “verbs” of the framework - they act on things - they do things.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Comparison to similar patterns&lt;br /&gt;
&lt;br /&gt;
Commands and Mementos have some similarity due to the fact they both work with an object's internal properties. The Command pattern keeps a record of changes to an object's state, and applies those changes in an ad-hoc fashion. A Memento also records changes to an object's state, and can restore that state at any time. The Chain of Responsibility pattern seems to handle processing in a similar manner to the Command, except it hands off processing to another process linearly unlike the Command pattern. An Interpreter pattern works in the example above because we are using language elements to determine which changes to apply at a given time.&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
Since the object that implements the command design doesn't know what function it does and what object it affects, as it acts like a black box, the implementation of the objects that is calling the black box can be changed and it can be implemented independently without affecting the flow of the program.&lt;br /&gt;
&lt;br /&gt;
Hence command pattern gives a wide range of flexibility and sophisticated design features.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
It complicates the application design. Every pattern complicates application design; they’re used because the benefits outweigh the cost. In fact, they should only be used when the benefits outweigh the costs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Javaworld&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
&lt;br /&gt;
According to Javaworld,&lt;br /&gt;
&lt;br /&gt;
The design patterns not only accelerate the design phase of an object-oriented (OO) project but also increase the productivity of the development team and quality of the software. &lt;br /&gt;
&lt;br /&gt;
They describe a Command pattern as an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. &lt;br /&gt;
&lt;br /&gt;
Sender -  invokes an operation&lt;br /&gt;
Receiver - receives the request to execute a certain operation. &lt;br /&gt;
Decoupling - the sender has no knowledge of the Receiver's interface&lt;br /&gt;
Request  - the command that is to be executed. &lt;br /&gt;
&lt;br /&gt;
Command pattern provides us flexibility as well as extensibility.&lt;br /&gt;
&lt;br /&gt;
The program discussed in the javaworld web directory is class Fan {&lt;br /&gt;
        public void startRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void stopRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is not rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Light {&lt;br /&gt;
        public void turnOn( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is on &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void turnOff( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is off&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Switch {&lt;br /&gt;
        private Command UpCommand, DownCommand;&lt;br /&gt;
        public Switch( Command Up, Command Down) {&lt;br /&gt;
                UpCommand = Up; // concrete Command registers itself with the invoker &lt;br /&gt;
                DownCommand = Down;&lt;br /&gt;
        }&lt;br /&gt;
        void flipUp( ) { // invoker calls back concrete Command, which executes the Command on the receiver &lt;br /&gt;
                        UpCommand . execute ( ) ;                           &lt;br /&gt;
        }&lt;br /&gt;
        void flipDown( ) {&lt;br /&gt;
                        DownCommand . execute ( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOnCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOnCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOn( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOffCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOffCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOff( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOnCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOnCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . startRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOffCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOffCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . stopRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
public class TestCommand {&lt;br /&gt;
                public static void main(String[] args) {&lt;br /&gt;
                        Light  testLight = new Light( );&lt;br /&gt;
                        LightOnCommand testLOC = new LightOnCommand(testLight);&lt;br /&gt;
                        LightOffCommand testLFC = new LightOffCommand(testLight);&lt;br /&gt;
                        Switch testSwitch = new Switch( testLOC,testLFC);       &lt;br /&gt;
                        testSwitch.flipUp( );&lt;br /&gt;
                        testSwitch.flipDown( );&lt;br /&gt;
                        Fan testFan = new Fan( );&lt;br /&gt;
                        FanOnCommand foc = new FanOnCommand(testFan);&lt;br /&gt;
                        FanOffCommand ffc = new FanOffCommand(testFan);&lt;br /&gt;
                        Switch ts = new Switch( foc,ffc);&lt;br /&gt;
                        ts.flipUp( );&lt;br /&gt;
                        ts.flipDown( ); &lt;br /&gt;
                }&lt;br /&gt;
}               &lt;br /&gt;
Command.java&lt;br /&gt;
public interface Command {&lt;br /&gt;
        public abstract void execute ( );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the above example,&lt;br /&gt;
The class Fan has startRotate() and stopRotate() methods,the class Light has turnOn() and turnOff() methods.&lt;br /&gt;
The catch here is startRotate of Fan and turnOn of Light is performed when the switch is turned on and stopRotate of Fan and turnOff of Light is performed when the switch in turned off.&lt;br /&gt;
&lt;br /&gt;
Keeping this real world catch, the class switch is created as a kind of interface for &lt;br /&gt;
fan and light to work on it turn on and turn off requests.&lt;br /&gt;
&lt;br /&gt;
The switch has flipUp and flipDown methods which acts like command design implementation.&lt;br /&gt;
The switch doesn't know what action it performs on flipUp and flipDown and what object is affected when flipUp and flipDown is pressed. &lt;br /&gt;
&lt;br /&gt;
Hence flipUp and flipDown acts like a command without knowing what function it does and what object it affects, but eventually performing the required action. That is the beauty of command design. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Command pattern has the following advantages:&lt;br /&gt;
1.	A command decouples the object that invokes the operation from the one that knows how to perform it.&lt;br /&gt;
2.	A command is a first-class object. It can be manipulated and extended like any other object.&lt;br /&gt;
3.	Commands can be assembled into a composite command.&lt;br /&gt;
4.	It's easy to add new commands, because you don't have to change the existing classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References&lt;br /&gt;
http://www.itsmedia.ca/the-command-pattern-and-restful-interfaces/&lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70570</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w45 pg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70570"/>
		<updated>2012-11-19T09:34:31Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Command Pattern =&lt;br /&gt;
&lt;br /&gt;
= Adapter Pattern, a directory of sites =&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Command Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Command Pattern.&lt;br /&gt;
&lt;br /&gt;
Introduction&lt;br /&gt;
&lt;br /&gt;
Definition: (http://en.wikipedia.org/wiki/Command_pattern)&lt;br /&gt;
&lt;br /&gt;
The command pattern is a behavioural design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
Three terms always associated with the command pattern are client, invoker and receiver. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b/&amp;gt;Client&lt;br /&gt;
The client instantiates the command object and provides the information required to call the method at a later time. &lt;br /&gt;
Invoker&lt;br /&gt;
The invoker decides when the method should be called. &lt;br /&gt;
Receiver &lt;br /&gt;
The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
How does it works?&lt;br /&gt;
Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.&lt;br /&gt;
 &lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
&lt;br /&gt;
Command Real world Example &lt;br /&gt;
(http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html)&lt;br /&gt;
&lt;br /&gt;
The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parameterized with different requests. The &amp;quot;check&amp;quot; at a diner is an example of a Command pattern. The waiter or waitress takes an order, or command from a customer, and encapsulates that order by writing it on the check. The order is then queued for a short order cook. Note that the pad of &amp;quot;checks&amp;quot; used by different diners is not dependent on the menu, and therefore they can support commands to cook many different items.&lt;br /&gt;
 &lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;br /&gt;
&lt;br /&gt;
When Would I Use This Pattern?&lt;br /&gt;
The Command Pattern is useful when:&lt;br /&gt;
•	A history of requests is needed&lt;br /&gt;
•	You need callback functionality&lt;br /&gt;
•	Requests need to be handled at variant times or in variant orders&lt;br /&gt;
•	The invoker should be decoupled from the object handling the invocation.&lt;br /&gt;
You'll see command being used a lot when you need to have multiple undo operations, where a stack of the recently executed commands are maintained. To implement the undo, all you need to do is get the last Command in the stack and execute it's undo() method.&lt;br /&gt;
You'll also find Command useful for wizards, progress bars, GUI buttons and menu actions, and other transactional behaviour. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Directory of sites&lt;br /&gt;
http://msdn.microsoft.com/en-us/magazine/cc163920.aspx&lt;br /&gt;
&lt;br /&gt;
Command pattern on MSDN Microsoft Website:&lt;br /&gt;
Command Pattern to -Simplify Distributed System Design Using the Command Pattern, MSMQ, and .NET &lt;br /&gt;
&lt;br /&gt;
Object-based collaboration VS service-based collaboration&lt;br /&gt;
Communication or exchange of request and service and the format or the pattern based on play an important criteria in successful implementation of  object-based collaboration and service-oriented architecture. &lt;br /&gt;
&lt;br /&gt;
Systems generally share a common platform, allowing you to optimize in ways you can't on widely dispersed -systems that span various operating systems and development environments are preferred to follow object-based collaboration than service-based collaboration.&lt;br /&gt;
&lt;br /&gt;
But generally the participants are hybrid and they don't share a common platform,  in such situations service oriented architecture comes handy. here the requests are queued in Microsoft Message Queuing (MSMQ) which implements Command design pattern. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The distributed command system discussed here is the combination of reliable messaging, services, and the Command pattern. While most often seen as a design pattern for implementing desktop application menu systems, the Command pattern offers advantages for distributed systems when combined with a robust messaging infrastructure like MSMQ. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Learning: &lt;br /&gt;
&lt;br /&gt;
The concept is very simple, in service oriented architecture, the participants don't share a common platform, hence you need a mechanism, a design pattern, a module to issue requests to objects.  &lt;br /&gt;
&lt;br /&gt;
The module that is doing the requesting doesn’t know what the receiver will be, or what operation will be requested.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Pattern and Participants&lt;br /&gt;
Class	Description&lt;br /&gt;
Command	Abstract base class for all Command objects&lt;br /&gt;
Concrete command	Any number of classes derived from Command&lt;br /&gt;
Client	The creator of a command&lt;br /&gt;
Invoker	Executes a command&lt;br /&gt;
Receiver	Any class that is acted on by a command&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example: &lt;br /&gt;
In ASP.NET, the database communication are based on the commands, the database commands such as create connection, command statement, parameter doesn't know what the object is and what the object it is requesting and these are typical example of command design pattern it best.&lt;br /&gt;
&lt;br /&gt;
Conclusion:&lt;br /&gt;
The Command pattern specifies a set of interactions that you can apply to the development of distributed systems in .NET. Use this pattern in your application to implement a high-performance, extendable backbone connecting your near link nodes that run .NET-enabled operating systems to achieve optimal performance and rapid development. Distributed commands are a mix of classic object orientation, messaging, and service architecture that together fill a definite need in the distributed systems space.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
The benefit of the Command class is that it makes it easier to add new functions to an application. &lt;br /&gt;
If we add a new capability, we no longer need to reopen the Controller class to code routing information. &lt;br /&gt;
Instead, we encapsulate the information in a Command object and assign it to whatever control we use in the View to invoke the new capability. &lt;br /&gt;
When the Command gets passed to the Controller, the Controller can use it to route the request, without knowing anything about it.&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. &lt;br /&gt;
&lt;br /&gt;
The routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. &lt;br /&gt;
&lt;br /&gt;
The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Web Application Component Toolkit (WACT)&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
&lt;br /&gt;
Web Application Component Toolkit&lt;br /&gt;
The Web Application Component Toolkit is a framework for creating web applications. WACT facilitates a modular approach where individual, independent or reusable components may be integrated into a larger web application. WACT assists in implementing the Model View Controller pattern and the related Domain Model, Template View, Front Controller and Application Controller patterns. WACT emphasizes writing secure web applications.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
WACT sees Command Pattern as&lt;br /&gt;
A GoF Design Pattern with the intent: “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”&lt;br /&gt;
&lt;br /&gt;
In WACT, this is seen with the FormController, where users can define a class which must contain the method performAction();&lt;br /&gt;
&lt;br /&gt;
class AddressForm extends PostFormController {&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
 &lt;br /&gt;
    function InitializeActions() {&lt;br /&gt;
        $this-&amp;gt;registerSubmitAction(&amp;quot;register&amp;quot;, &amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
        $this-&amp;gt;registerDefaultSubmitAction(&amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The registerSubmitAction call above points the controller at the class RegisterUser, which is where the Command pattern resides.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class RegisterUser {&lt;br /&gt;
 &lt;br /&gt;
    function performAction(&amp;amp;$context) {&lt;br /&gt;
        $DataSpace =&amp;amp; $context-&amp;gt;getDataSpace();&lt;br /&gt;
        $Page =&amp;amp; new Template('/input/thanks.html');&lt;br /&gt;
        $Page-&amp;gt;import($DataSpace-&amp;gt;export());&lt;br /&gt;
        $Page-&amp;gt;display();&lt;br /&gt;
        return FORM_COMPLETE;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The FormController expects to find the performAction method in the Action Class RegisterUser, calling it when the form is submitted (and successfully validated).&lt;br /&gt;
&lt;br /&gt;
The Command pattern is a Gang of Four Design Pattern, used to allow a function name to be wrapped up in a class namespace, so that some code, which will call the function, can be given an instance of one of many classes without caring which which instance it’s dealing with.&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
Here the Form controller in its post functionality exhibits command pattern as it requires the object that calls the post form functionality to implement performAction method, the actions that the object needs to perform when the form is submitted, but the post form controller doesn't know what are actions performed by the performAction method and whose performAction  method is called all it requires is it needs a class that has the performAction method to post it forms. &lt;br /&gt;
Hence the performAction acts like a command that is being called whenever the form is posted.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example:&lt;br /&gt;
PHP example &lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
/* interface */ class Tag {&lt;br /&gt;
   // The abstract command method&lt;br /&gt;
   function render($content);&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Bold {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;b&amp;gt;$content&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Italic {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;i&amp;gt;$content&amp;lt;/i&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
$commands = array (&lt;br /&gt;
    new Bold(),new Italic(), new Italic(), new Bold()&lt;br /&gt;
);&lt;br /&gt;
 &lt;br /&gt;
// Iterate over the command objects&lt;br /&gt;
foreach ( $commands as $command ) {&lt;br /&gt;
    echo ( $command-&amp;gt;render('Hello World!') );&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the PHP example provided above, class Bold, Italic works on the function render.&lt;br /&gt;
&lt;br /&gt;
In class Tag, all it does is for each command in the command object, render functionality is called, the render doesn't know which command, bold or italics is called and what doesn't knows the functionality of command (bold or italics) &lt;br /&gt;
&lt;br /&gt;
Applications&lt;br /&gt;
&lt;br /&gt;
The Command pattern is used widely on in web based application frameworks like Java Struts from a PHP Perspective, usually in Action Classes, to allow framework users to define logic which responds to “events” in the application. Action Classes can be regarded as the “verbs” of the framework - they act on things - they do things.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Comparison to similar patterns&lt;br /&gt;
&lt;br /&gt;
Commands and Mementos have some similarity due to the fact they both work with an object's internal properties. The Command pattern keeps a record of changes to an object's state, and applies those changes in an ad-hoc fashion. A Memento also records changes to an object's state, and can restore that state at any time. The Chain of Responsibility pattern seems to handle processing in a similar manner to the Command, except it hands off processing to another process linearly unlike the Command pattern. An Interpreter pattern works in the example above because we are using language elements to determine which changes to apply at a given time.&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
Since the object that implements the command design doesn't know what function it does and what object it affects, as it acts like a black box, the implementation of the objects that is calling the black box can be changed and it can be implemented independently without affecting the flow of the program.&lt;br /&gt;
&lt;br /&gt;
Hence command pattern gives a wide range of flexibility and sophisticated design features.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
It complicates the application design. Every pattern complicates application design; they’re used because the benefits outweigh the cost. In fact, they should only be used when the benefits outweigh the costs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Javaworld&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
&lt;br /&gt;
According to Javaworld,&lt;br /&gt;
&lt;br /&gt;
The design patterns not only accelerate the design phase of an object-oriented (OO) project but also increase the productivity of the development team and quality of the software. &lt;br /&gt;
&lt;br /&gt;
They describe a Command pattern as an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. &lt;br /&gt;
&lt;br /&gt;
Sender -  invokes an operation&lt;br /&gt;
Receiver - receives the request to execute a certain operation. &lt;br /&gt;
Decoupling - the sender has no knowledge of the Receiver's interface&lt;br /&gt;
Request  - the command that is to be executed. &lt;br /&gt;
&lt;br /&gt;
Command pattern provides us flexibility as well as extensibility.&lt;br /&gt;
&lt;br /&gt;
The program discussed in the javaworld web directory is class Fan {&lt;br /&gt;
        public void startRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void stopRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is not rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Light {&lt;br /&gt;
        public void turnOn( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is on &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void turnOff( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is off&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Switch {&lt;br /&gt;
        private Command UpCommand, DownCommand;&lt;br /&gt;
        public Switch( Command Up, Command Down) {&lt;br /&gt;
                UpCommand = Up; // concrete Command registers itself with the invoker &lt;br /&gt;
                DownCommand = Down;&lt;br /&gt;
        }&lt;br /&gt;
        void flipUp( ) { // invoker calls back concrete Command, which executes the Command on the receiver &lt;br /&gt;
                        UpCommand . execute ( ) ;                           &lt;br /&gt;
        }&lt;br /&gt;
        void flipDown( ) {&lt;br /&gt;
                        DownCommand . execute ( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOnCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOnCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOn( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOffCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOffCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOff( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOnCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOnCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . startRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOffCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOffCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . stopRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
public class TestCommand {&lt;br /&gt;
                public static void main(String[] args) {&lt;br /&gt;
                        Light  testLight = new Light( );&lt;br /&gt;
                        LightOnCommand testLOC = new LightOnCommand(testLight);&lt;br /&gt;
                        LightOffCommand testLFC = new LightOffCommand(testLight);&lt;br /&gt;
                        Switch testSwitch = new Switch( testLOC,testLFC);       &lt;br /&gt;
                        testSwitch.flipUp( );&lt;br /&gt;
                        testSwitch.flipDown( );&lt;br /&gt;
                        Fan testFan = new Fan( );&lt;br /&gt;
                        FanOnCommand foc = new FanOnCommand(testFan);&lt;br /&gt;
                        FanOffCommand ffc = new FanOffCommand(testFan);&lt;br /&gt;
                        Switch ts = new Switch( foc,ffc);&lt;br /&gt;
                        ts.flipUp( );&lt;br /&gt;
                        ts.flipDown( ); &lt;br /&gt;
                }&lt;br /&gt;
}               &lt;br /&gt;
Command.java&lt;br /&gt;
public interface Command {&lt;br /&gt;
        public abstract void execute ( );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the above example,&lt;br /&gt;
The class Fan has startRotate() and stopRotate() methods,the class Light has turnOn() and turnOff() methods.&lt;br /&gt;
The catch here is startRotate of Fan and turnOn of Light is performed when the switch is turned on and stopRotate of Fan and turnOff of Light is performed when the switch in turned off.&lt;br /&gt;
&lt;br /&gt;
Keeping this real world catch, the class switch is created as a kind of interface for &lt;br /&gt;
fan and light to work on it turn on and turn off requests.&lt;br /&gt;
&lt;br /&gt;
The switch has flipUp and flipDown methods which acts like command design implementation.&lt;br /&gt;
The switch doesn't know what action it performs on flipUp and flipDown and what object is affected when flipUp and flipDown is pressed. &lt;br /&gt;
&lt;br /&gt;
Hence flipUp and flipDown acts like a command without knowing what function it does and what object it affects, but eventually performing the required action. That is the beauty of command design. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Command pattern has the following advantages:&lt;br /&gt;
1.	A command decouples the object that invokes the operation from the one that knows how to perform it.&lt;br /&gt;
2.	A command is a first-class object. It can be manipulated and extended like any other object.&lt;br /&gt;
3.	Commands can be assembled into a composite command.&lt;br /&gt;
4.	It's easy to add new commands, because you don't have to change the existing classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References&lt;br /&gt;
http://www.itsmedia.ca/the-command-pattern-and-restful-interfaces/&lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70569</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w45 pg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70569"/>
		<updated>2012-11-19T09:33:13Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Command Pattern =&lt;br /&gt;
&lt;br /&gt;
= Adapter Pattern, a directory of sites =&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Command Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Command Pattern.&lt;br /&gt;
&lt;br /&gt;
Introduction&lt;br /&gt;
&lt;br /&gt;
Definition: (http://en.wikipedia.org/wiki/Command_pattern)&lt;br /&gt;
&lt;br /&gt;
The command pattern is a behavioural design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
Three terms always associated with the command pattern are client, invoker and receiver. &lt;br /&gt;
&lt;br /&gt;
Client&lt;br /&gt;
The client instantiates the command object and provides the information required to call the method at a later time. &lt;br /&gt;
Invoker&lt;br /&gt;
The invoker decides when the method should be called. &lt;br /&gt;
Receiver &lt;br /&gt;
The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
How does it works?&lt;br /&gt;
Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.&lt;br /&gt;
 &lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
&lt;br /&gt;
Command Real world Example &lt;br /&gt;
(http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html)&lt;br /&gt;
&lt;br /&gt;
The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parameterized with different requests. The &amp;quot;check&amp;quot; at a diner is an example of a Command pattern. The waiter or waitress takes an order, or command from a customer, and encapsulates that order by writing it on the check. The order is then queued for a short order cook. Note that the pad of &amp;quot;checks&amp;quot; used by different diners is not dependent on the menu, and therefore they can support commands to cook many different items.&lt;br /&gt;
 &lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;br /&gt;
&lt;br /&gt;
When Would I Use This Pattern?&lt;br /&gt;
The Command Pattern is useful when:&lt;br /&gt;
•	A history of requests is needed&lt;br /&gt;
•	You need callback functionality&lt;br /&gt;
•	Requests need to be handled at variant times or in variant orders&lt;br /&gt;
•	The invoker should be decoupled from the object handling the invocation.&lt;br /&gt;
You'll see command being used a lot when you need to have multiple undo operations, where a stack of the recently executed commands are maintained. To implement the undo, all you need to do is get the last Command in the stack and execute it's undo() method.&lt;br /&gt;
You'll also find Command useful for wizards, progress bars, GUI buttons and menu actions, and other transactional behaviour. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Directory of sites&lt;br /&gt;
http://msdn.microsoft.com/en-us/magazine/cc163920.aspx&lt;br /&gt;
&lt;br /&gt;
Command pattern on MSDN Microsoft Website:&lt;br /&gt;
Command Pattern to -Simplify Distributed System Design Using the Command Pattern, MSMQ, and .NET &lt;br /&gt;
&lt;br /&gt;
Object-based collaboration VS service-based collaboration&lt;br /&gt;
Communication or exchange of request and service and the format or the pattern based on play an important criteria in successful implementation of  object-based collaboration and service-oriented architecture. &lt;br /&gt;
&lt;br /&gt;
Systems generally share a common platform, allowing you to optimize in ways you can't on widely dispersed -systems that span various operating systems and development environments are preferred to follow object-based collaboration than service-based collaboration.&lt;br /&gt;
&lt;br /&gt;
But generally the participants are hybrid and they don't share a common platform,  in such situations service oriented architecture comes handy. here the requests are queued in Microsoft Message Queuing (MSMQ) which implements Command design pattern. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The distributed command system discussed here is the combination of reliable messaging, services, and the Command pattern. While most often seen as a design pattern for implementing desktop application menu systems, the Command pattern offers advantages for distributed systems when combined with a robust messaging infrastructure like MSMQ. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Learning: &lt;br /&gt;
&lt;br /&gt;
The concept is very simple, in service oriented architecture, the participants don't share a common platform, hence you need a mechanism, a design pattern, a module to issue requests to objects.  &lt;br /&gt;
&lt;br /&gt;
The module that is doing the requesting doesn’t know what the receiver will be, or what operation will be requested.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Pattern and Participants&lt;br /&gt;
Class	Description&lt;br /&gt;
Command	Abstract base class for all Command objects&lt;br /&gt;
Concrete command	Any number of classes derived from Command&lt;br /&gt;
Client	The creator of a command&lt;br /&gt;
Invoker	Executes a command&lt;br /&gt;
Receiver	Any class that is acted on by a command&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example: &lt;br /&gt;
In ASP.NET, the database communication are based on the commands, the database commands such as create connection, command statement, parameter doesn't know what the object is and what the object it is requesting and these are typical example of command design pattern it best.&lt;br /&gt;
&lt;br /&gt;
Conclusion:&lt;br /&gt;
The Command pattern specifies a set of interactions that you can apply to the development of distributed systems in .NET. Use this pattern in your application to implement a high-performance, extendable backbone connecting your near link nodes that run .NET-enabled operating systems to achieve optimal performance and rapid development. Distributed commands are a mix of classic object orientation, messaging, and service architecture that together fill a definite need in the distributed systems space.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
The benefit of the Command class is that it makes it easier to add new functions to an application. &lt;br /&gt;
If we add a new capability, we no longer need to reopen the Controller class to code routing information. &lt;br /&gt;
Instead, we encapsulate the information in a Command object and assign it to whatever control we use in the View to invoke the new capability. &lt;br /&gt;
When the Command gets passed to the Controller, the Controller can use it to route the request, without knowing anything about it.&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. &lt;br /&gt;
&lt;br /&gt;
The routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. &lt;br /&gt;
&lt;br /&gt;
The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Web Application Component Toolkit (WACT)&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
&lt;br /&gt;
Web Application Component Toolkit&lt;br /&gt;
The Web Application Component Toolkit is a framework for creating web applications. WACT facilitates a modular approach where individual, independent or reusable components may be integrated into a larger web application. WACT assists in implementing the Model View Controller pattern and the related Domain Model, Template View, Front Controller and Application Controller patterns. WACT emphasizes writing secure web applications.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
WACT sees Command Pattern as&lt;br /&gt;
A GoF Design Pattern with the intent: “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”&lt;br /&gt;
&lt;br /&gt;
In WACT, this is seen with the FormController, where users can define a class which must contain the method performAction();&lt;br /&gt;
&lt;br /&gt;
class AddressForm extends PostFormController {&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
 &lt;br /&gt;
    function InitializeActions() {&lt;br /&gt;
        $this-&amp;gt;registerSubmitAction(&amp;quot;register&amp;quot;, &amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
        $this-&amp;gt;registerDefaultSubmitAction(&amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The registerSubmitAction call above points the controller at the class RegisterUser, which is where the Command pattern resides.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class RegisterUser {&lt;br /&gt;
 &lt;br /&gt;
    function performAction(&amp;amp;$context) {&lt;br /&gt;
        $DataSpace =&amp;amp; $context-&amp;gt;getDataSpace();&lt;br /&gt;
        $Page =&amp;amp; new Template('/input/thanks.html');&lt;br /&gt;
        $Page-&amp;gt;import($DataSpace-&amp;gt;export());&lt;br /&gt;
        $Page-&amp;gt;display();&lt;br /&gt;
        return FORM_COMPLETE;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The FormController expects to find the performAction method in the Action Class RegisterUser, calling it when the form is submitted (and successfully validated).&lt;br /&gt;
&lt;br /&gt;
The Command pattern is a Gang of Four Design Pattern, used to allow a function name to be wrapped up in a class namespace, so that some code, which will call the function, can be given an instance of one of many classes without caring which which instance it’s dealing with.&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
Here the Form controller in its post functionality exhibits command pattern as it requires the object that calls the post form functionality to implement performAction method, the actions that the object needs to perform when the form is submitted, but the post form controller doesn't know what are actions performed by the performAction method and whose performAction  method is called all it requires is it needs a class that has the performAction method to post it forms. &lt;br /&gt;
Hence the performAction acts like a command that is being called whenever the form is posted.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example:&lt;br /&gt;
PHP example &lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
/* interface */ class Tag {&lt;br /&gt;
   // The abstract command method&lt;br /&gt;
   function render($content);&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Bold {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;b&amp;gt;$content&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Italic {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;i&amp;gt;$content&amp;lt;/i&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
$commands = array (&lt;br /&gt;
    new Bold(),new Italic(), new Italic(), new Bold()&lt;br /&gt;
);&lt;br /&gt;
 &lt;br /&gt;
// Iterate over the command objects&lt;br /&gt;
foreach ( $commands as $command ) {&lt;br /&gt;
    echo ( $command-&amp;gt;render('Hello World!') );&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the PHP example provided above, class Bold, Italic works on the function render.&lt;br /&gt;
&lt;br /&gt;
In class Tag, all it does is for each command in the command object, render functionality is called, the render doesn't know which command, bold or italics is called and what doesn't knows the functionality of command (bold or italics) &lt;br /&gt;
&lt;br /&gt;
Applications&lt;br /&gt;
&lt;br /&gt;
The Command pattern is used widely on in web based application frameworks like Java Struts from a PHP Perspective, usually in Action Classes, to allow framework users to define logic which responds to “events” in the application. Action Classes can be regarded as the “verbs” of the framework - they act on things - they do things.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Comparison to similar patterns&lt;br /&gt;
&lt;br /&gt;
Commands and Mementos have some similarity due to the fact they both work with an object's internal properties. The Command pattern keeps a record of changes to an object's state, and applies those changes in an ad-hoc fashion. A Memento also records changes to an object's state, and can restore that state at any time. The Chain of Responsibility pattern seems to handle processing in a similar manner to the Command, except it hands off processing to another process linearly unlike the Command pattern. An Interpreter pattern works in the example above because we are using language elements to determine which changes to apply at a given time.&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
Since the object that implements the command design doesn't know what function it does and what object it affects, as it acts like a black box, the implementation of the objects that is calling the black box can be changed and it can be implemented independently without affecting the flow of the program.&lt;br /&gt;
&lt;br /&gt;
Hence command pattern gives a wide range of flexibility and sophisticated design features.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
It complicates the application design. Every pattern complicates application design; they’re used because the benefits outweigh the cost. In fact, they should only be used when the benefits outweigh the costs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Javaworld&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
&lt;br /&gt;
According to Javaworld,&lt;br /&gt;
&lt;br /&gt;
The design patterns not only accelerate the design phase of an object-oriented (OO) project but also increase the productivity of the development team and quality of the software. &lt;br /&gt;
&lt;br /&gt;
They describe a Command pattern as an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. &lt;br /&gt;
&lt;br /&gt;
Sender -  invokes an operation&lt;br /&gt;
Receiver - receives the request to execute a certain operation. &lt;br /&gt;
Decoupling - the sender has no knowledge of the Receiver's interface&lt;br /&gt;
Request  - the command that is to be executed. &lt;br /&gt;
&lt;br /&gt;
Command pattern provides us flexibility as well as extensibility.&lt;br /&gt;
&lt;br /&gt;
The program discussed in the javaworld web directory is class Fan {&lt;br /&gt;
        public void startRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void stopRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is not rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Light {&lt;br /&gt;
        public void turnOn( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is on &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void turnOff( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is off&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Switch {&lt;br /&gt;
        private Command UpCommand, DownCommand;&lt;br /&gt;
        public Switch( Command Up, Command Down) {&lt;br /&gt;
                UpCommand = Up; // concrete Command registers itself with the invoker &lt;br /&gt;
                DownCommand = Down;&lt;br /&gt;
        }&lt;br /&gt;
        void flipUp( ) { // invoker calls back concrete Command, which executes the Command on the receiver &lt;br /&gt;
                        UpCommand . execute ( ) ;                           &lt;br /&gt;
        }&lt;br /&gt;
        void flipDown( ) {&lt;br /&gt;
                        DownCommand . execute ( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOnCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOnCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOn( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOffCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOffCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOff( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOnCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOnCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . startRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOffCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOffCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . stopRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
public class TestCommand {&lt;br /&gt;
                public static void main(String[] args) {&lt;br /&gt;
                        Light  testLight = new Light( );&lt;br /&gt;
                        LightOnCommand testLOC = new LightOnCommand(testLight);&lt;br /&gt;
                        LightOffCommand testLFC = new LightOffCommand(testLight);&lt;br /&gt;
                        Switch testSwitch = new Switch( testLOC,testLFC);       &lt;br /&gt;
                        testSwitch.flipUp( );&lt;br /&gt;
                        testSwitch.flipDown( );&lt;br /&gt;
                        Fan testFan = new Fan( );&lt;br /&gt;
                        FanOnCommand foc = new FanOnCommand(testFan);&lt;br /&gt;
                        FanOffCommand ffc = new FanOffCommand(testFan);&lt;br /&gt;
                        Switch ts = new Switch( foc,ffc);&lt;br /&gt;
                        ts.flipUp( );&lt;br /&gt;
                        ts.flipDown( ); &lt;br /&gt;
                }&lt;br /&gt;
}               &lt;br /&gt;
Command.java&lt;br /&gt;
public interface Command {&lt;br /&gt;
        public abstract void execute ( );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the above example,&lt;br /&gt;
The class Fan has startRotate() and stopRotate() methods,the class Light has turnOn() and turnOff() methods.&lt;br /&gt;
The catch here is startRotate of Fan and turnOn of Light is performed when the switch is turned on and stopRotate of Fan and turnOff of Light is performed when the switch in turned off.&lt;br /&gt;
&lt;br /&gt;
Keeping this real world catch, the class switch is created as a kind of interface for &lt;br /&gt;
fan and light to work on it turn on and turn off requests.&lt;br /&gt;
&lt;br /&gt;
The switch has flipUp and flipDown methods which acts like command design implementation.&lt;br /&gt;
The switch doesn't know what action it performs on flipUp and flipDown and what object is affected when flipUp and flipDown is pressed. &lt;br /&gt;
&lt;br /&gt;
Hence flipUp and flipDown acts like a command without knowing what function it does and what object it affects, but eventually performing the required action. That is the beauty of command design. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Command pattern has the following advantages:&lt;br /&gt;
1.	A command decouples the object that invokes the operation from the one that knows how to perform it.&lt;br /&gt;
2.	A command is a first-class object. It can be manipulated and extended like any other object.&lt;br /&gt;
3.	Commands can be assembled into a composite command.&lt;br /&gt;
4.	It's easy to add new commands, because you don't have to change the existing classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References&lt;br /&gt;
http://www.itsmedia.ca/the-command-pattern-and-restful-interfaces/&lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70568</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w45 pg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70568"/>
		<updated>2012-11-19T09:32:17Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Command Pattern=&lt;br /&gt;
&lt;br /&gt;
=Adapter Pattern, a directory of sites=&lt;br /&gt;
&lt;br /&gt;
This wiki page provides a directory of sites for Command Design Pattern that will help readers to know what each website contains, why should readers refer one website over another and a list of examples that readers can look up to understand the Command Pattern.&lt;br /&gt;
&lt;br /&gt;
Introduction&lt;br /&gt;
&lt;br /&gt;
Definition: (http://en.wikipedia.org/wiki/Command_pattern)&lt;br /&gt;
&lt;br /&gt;
The command pattern is a behavioural design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
Three terms always associated with the command pattern are client, invoker and receiver. &lt;br /&gt;
&lt;br /&gt;
Client&lt;br /&gt;
The client instantiates the command object and provides the information required to call the method at a later time. &lt;br /&gt;
Invoker&lt;br /&gt;
The invoker decides when the method should be called. &lt;br /&gt;
Receiver &lt;br /&gt;
The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
How does it works?&lt;br /&gt;
Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.&lt;br /&gt;
 &lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
&lt;br /&gt;
Command Real world Example &lt;br /&gt;
(http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html)&lt;br /&gt;
&lt;br /&gt;
The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parameterized with different requests. The &amp;quot;check&amp;quot; at a diner is an example of a Command pattern. The waiter or waitress takes an order, or command from a customer, and encapsulates that order by writing it on the check. The order is then queued for a short order cook. Note that the pad of &amp;quot;checks&amp;quot; used by different diners is not dependent on the menu, and therefore they can support commands to cook many different items.&lt;br /&gt;
 &lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;br /&gt;
&lt;br /&gt;
When Would I Use This Pattern?&lt;br /&gt;
The Command Pattern is useful when:&lt;br /&gt;
•	A history of requests is needed&lt;br /&gt;
•	You need callback functionality&lt;br /&gt;
•	Requests need to be handled at variant times or in variant orders&lt;br /&gt;
•	The invoker should be decoupled from the object handling the invocation.&lt;br /&gt;
You'll see command being used a lot when you need to have multiple undo operations, where a stack of the recently executed commands are maintained. To implement the undo, all you need to do is get the last Command in the stack and execute it's undo() method.&lt;br /&gt;
You'll also find Command useful for wizards, progress bars, GUI buttons and menu actions, and other transactional behaviour. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Directory of sites&lt;br /&gt;
http://msdn.microsoft.com/en-us/magazine/cc163920.aspx&lt;br /&gt;
&lt;br /&gt;
Command pattern on MSDN Microsoft Website:&lt;br /&gt;
Command Pattern to -Simplify Distributed System Design Using the Command Pattern, MSMQ, and .NET &lt;br /&gt;
&lt;br /&gt;
Object-based collaboration VS service-based collaboration&lt;br /&gt;
Communication or exchange of request and service and the format or the pattern based on play an important criteria in successful implementation of  object-based collaboration and service-oriented architecture. &lt;br /&gt;
&lt;br /&gt;
Systems generally share a common platform, allowing you to optimize in ways you can't on widely dispersed -systems that span various operating systems and development environments are preferred to follow object-based collaboration than service-based collaboration.&lt;br /&gt;
&lt;br /&gt;
But generally the participants are hybrid and they don't share a common platform,  in such situations service oriented architecture comes handy. here the requests are queued in Microsoft Message Queuing (MSMQ) which implements Command design pattern. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The distributed command system discussed here is the combination of reliable messaging, services, and the Command pattern. While most often seen as a design pattern for implementing desktop application menu systems, the Command pattern offers advantages for distributed systems when combined with a robust messaging infrastructure like MSMQ. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Learning: &lt;br /&gt;
&lt;br /&gt;
The concept is very simple, in service oriented architecture, the participants don't share a common platform, hence you need a mechanism, a design pattern, a module to issue requests to objects.  &lt;br /&gt;
&lt;br /&gt;
The module that is doing the requesting doesn’t know what the receiver will be, or what operation will be requested.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Pattern and Participants&lt;br /&gt;
Class	Description&lt;br /&gt;
Command	Abstract base class for all Command objects&lt;br /&gt;
Concrete command	Any number of classes derived from Command&lt;br /&gt;
Client	The creator of a command&lt;br /&gt;
Invoker	Executes a command&lt;br /&gt;
Receiver	Any class that is acted on by a command&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example: &lt;br /&gt;
In ASP.NET, the database communication are based on the commands, the database commands such as create connection, command statement, parameter doesn't know what the object is and what the object it is requesting and these are typical example of command design pattern it best.&lt;br /&gt;
&lt;br /&gt;
Conclusion:&lt;br /&gt;
The Command pattern specifies a set of interactions that you can apply to the development of distributed systems in .NET. Use this pattern in your application to implement a high-performance, extendable backbone connecting your near link nodes that run .NET-enabled operating systems to achieve optimal performance and rapid development. Distributed commands are a mix of classic object orientation, messaging, and service architecture that together fill a definite need in the distributed systems space.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
The benefit of the Command class is that it makes it easier to add new functions to an application. &lt;br /&gt;
If we add a new capability, we no longer need to reopen the Controller class to code routing information. &lt;br /&gt;
Instead, we encapsulate the information in a Command object and assign it to whatever control we use in the View to invoke the new capability. &lt;br /&gt;
When the Command gets passed to the Controller, the Controller can use it to route the request, without knowing anything about it.&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. &lt;br /&gt;
&lt;br /&gt;
The routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. &lt;br /&gt;
&lt;br /&gt;
The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Web Application Component Toolkit (WACT)&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
&lt;br /&gt;
Web Application Component Toolkit&lt;br /&gt;
The Web Application Component Toolkit is a framework for creating web applications. WACT facilitates a modular approach where individual, independent or reusable components may be integrated into a larger web application. WACT assists in implementing the Model View Controller pattern and the related Domain Model, Template View, Front Controller and Application Controller patterns. WACT emphasizes writing secure web applications.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
WACT sees Command Pattern as&lt;br /&gt;
A GoF Design Pattern with the intent: “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”&lt;br /&gt;
&lt;br /&gt;
In WACT, this is seen with the FormController, where users can define a class which must contain the method performAction();&lt;br /&gt;
&lt;br /&gt;
class AddressForm extends PostFormController {&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
 &lt;br /&gt;
    function InitializeActions() {&lt;br /&gt;
        $this-&amp;gt;registerSubmitAction(&amp;quot;register&amp;quot;, &amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
        $this-&amp;gt;registerDefaultSubmitAction(&amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The registerSubmitAction call above points the controller at the class RegisterUser, which is where the Command pattern resides.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class RegisterUser {&lt;br /&gt;
 &lt;br /&gt;
    function performAction(&amp;amp;$context) {&lt;br /&gt;
        $DataSpace =&amp;amp; $context-&amp;gt;getDataSpace();&lt;br /&gt;
        $Page =&amp;amp; new Template('/input/thanks.html');&lt;br /&gt;
        $Page-&amp;gt;import($DataSpace-&amp;gt;export());&lt;br /&gt;
        $Page-&amp;gt;display();&lt;br /&gt;
        return FORM_COMPLETE;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The FormController expects to find the performAction method in the Action Class RegisterUser, calling it when the form is submitted (and successfully validated).&lt;br /&gt;
&lt;br /&gt;
The Command pattern is a Gang of Four Design Pattern, used to allow a function name to be wrapped up in a class namespace, so that some code, which will call the function, can be given an instance of one of many classes without caring which which instance it’s dealing with.&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
Here the Form controller in its post functionality exhibits command pattern as it requires the object that calls the post form functionality to implement performAction method, the actions that the object needs to perform when the form is submitted, but the post form controller doesn't know what are actions performed by the performAction method and whose performAction  method is called all it requires is it needs a class that has the performAction method to post it forms. &lt;br /&gt;
Hence the performAction acts like a command that is being called whenever the form is posted.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example:&lt;br /&gt;
PHP example &lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
/* interface */ class Tag {&lt;br /&gt;
   // The abstract command method&lt;br /&gt;
   function render($content);&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Bold {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;b&amp;gt;$content&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Italic {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;i&amp;gt;$content&amp;lt;/i&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
$commands = array (&lt;br /&gt;
    new Bold(),new Italic(), new Italic(), new Bold()&lt;br /&gt;
);&lt;br /&gt;
 &lt;br /&gt;
// Iterate over the command objects&lt;br /&gt;
foreach ( $commands as $command ) {&lt;br /&gt;
    echo ( $command-&amp;gt;render('Hello World!') );&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the PHP example provided above, class Bold, Italic works on the function render.&lt;br /&gt;
&lt;br /&gt;
In class Tag, all it does is for each command in the command object, render functionality is called, the render doesn't know which command, bold or italics is called and what doesn't knows the functionality of command (bold or italics) &lt;br /&gt;
&lt;br /&gt;
Applications&lt;br /&gt;
&lt;br /&gt;
The Command pattern is used widely on in web based application frameworks like Java Struts from a PHP Perspective, usually in Action Classes, to allow framework users to define logic which responds to “events” in the application. Action Classes can be regarded as the “verbs” of the framework - they act on things - they do things.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Comparison to similar patterns&lt;br /&gt;
&lt;br /&gt;
Commands and Mementos have some similarity due to the fact they both work with an object's internal properties. The Command pattern keeps a record of changes to an object's state, and applies those changes in an ad-hoc fashion. A Memento also records changes to an object's state, and can restore that state at any time. The Chain of Responsibility pattern seems to handle processing in a similar manner to the Command, except it hands off processing to another process linearly unlike the Command pattern. An Interpreter pattern works in the example above because we are using language elements to determine which changes to apply at a given time.&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
Since the object that implements the command design doesn't know what function it does and what object it affects, as it acts like a black box, the implementation of the objects that is calling the black box can be changed and it can be implemented independently without affecting the flow of the program.&lt;br /&gt;
&lt;br /&gt;
Hence command pattern gives a wide range of flexibility and sophisticated design features.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
It complicates the application design. Every pattern complicates application design; they’re used because the benefits outweigh the cost. In fact, they should only be used when the benefits outweigh the costs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Javaworld&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
&lt;br /&gt;
According to Javaworld,&lt;br /&gt;
&lt;br /&gt;
The design patterns not only accelerate the design phase of an object-oriented (OO) project but also increase the productivity of the development team and quality of the software. &lt;br /&gt;
&lt;br /&gt;
They describe a Command pattern as an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. &lt;br /&gt;
&lt;br /&gt;
Sender -  invokes an operation&lt;br /&gt;
Receiver - receives the request to execute a certain operation. &lt;br /&gt;
Decoupling - the sender has no knowledge of the Receiver's interface&lt;br /&gt;
Request  - the command that is to be executed. &lt;br /&gt;
&lt;br /&gt;
Command pattern provides us flexibility as well as extensibility.&lt;br /&gt;
&lt;br /&gt;
The program discussed in the javaworld web directory is class Fan {&lt;br /&gt;
        public void startRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void stopRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is not rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Light {&lt;br /&gt;
        public void turnOn( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is on &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void turnOff( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is off&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Switch {&lt;br /&gt;
        private Command UpCommand, DownCommand;&lt;br /&gt;
        public Switch( Command Up, Command Down) {&lt;br /&gt;
                UpCommand = Up; // concrete Command registers itself with the invoker &lt;br /&gt;
                DownCommand = Down;&lt;br /&gt;
        }&lt;br /&gt;
        void flipUp( ) { // invoker calls back concrete Command, which executes the Command on the receiver &lt;br /&gt;
                        UpCommand . execute ( ) ;                           &lt;br /&gt;
        }&lt;br /&gt;
        void flipDown( ) {&lt;br /&gt;
                        DownCommand . execute ( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOnCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOnCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOn( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOffCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOffCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOff( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOnCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOnCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . startRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOffCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOffCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . stopRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
public class TestCommand {&lt;br /&gt;
                public static void main(String[] args) {&lt;br /&gt;
                        Light  testLight = new Light( );&lt;br /&gt;
                        LightOnCommand testLOC = new LightOnCommand(testLight);&lt;br /&gt;
                        LightOffCommand testLFC = new LightOffCommand(testLight);&lt;br /&gt;
                        Switch testSwitch = new Switch( testLOC,testLFC);       &lt;br /&gt;
                        testSwitch.flipUp( );&lt;br /&gt;
                        testSwitch.flipDown( );&lt;br /&gt;
                        Fan testFan = new Fan( );&lt;br /&gt;
                        FanOnCommand foc = new FanOnCommand(testFan);&lt;br /&gt;
                        FanOffCommand ffc = new FanOffCommand(testFan);&lt;br /&gt;
                        Switch ts = new Switch( foc,ffc);&lt;br /&gt;
                        ts.flipUp( );&lt;br /&gt;
                        ts.flipDown( ); &lt;br /&gt;
                }&lt;br /&gt;
}               &lt;br /&gt;
Command.java&lt;br /&gt;
public interface Command {&lt;br /&gt;
        public abstract void execute ( );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the above example,&lt;br /&gt;
The class Fan has startRotate() and stopRotate() methods,the class Light has turnOn() and turnOff() methods.&lt;br /&gt;
The catch here is startRotate of Fan and turnOn of Light is performed when the switch is turned on and stopRotate of Fan and turnOff of Light is performed when the switch in turned off.&lt;br /&gt;
&lt;br /&gt;
Keeping this real world catch, the class switch is created as a kind of interface for &lt;br /&gt;
fan and light to work on it turn on and turn off requests.&lt;br /&gt;
&lt;br /&gt;
The switch has flipUp and flipDown methods which acts like command design implementation.&lt;br /&gt;
The switch doesn't know what action it performs on flipUp and flipDown and what object is affected when flipUp and flipDown is pressed. &lt;br /&gt;
&lt;br /&gt;
Hence flipUp and flipDown acts like a command without knowing what function it does and what object it affects, but eventually performing the required action. That is the beauty of command design. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Command pattern has the following advantages:&lt;br /&gt;
1.	A command decouples the object that invokes the operation from the one that knows how to perform it.&lt;br /&gt;
2.	A command is a first-class object. It can be manipulated and extended like any other object.&lt;br /&gt;
3.	Commands can be assembled into a composite command.&lt;br /&gt;
4.	It's easy to add new commands, because you don't have to change the existing classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References&lt;br /&gt;
http://www.itsmedia.ca/the-command-pattern-and-restful-interfaces/&lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=70567</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=70567"/>
		<updated>2012-11-19T09:29:11Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby [http://en.wikipedia.org/wiki/Mixin Modules] are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Unlike [http://en.wikipedia.org/wiki/Object-oriented_programming classes], [http://en.wikipedia.org/wiki/Object-oriented_programming 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;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &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.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby [http://ruby.about.com/od/rubyfeatures/a/require.htm require] statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The most interesting fact about the use of modules is to define [http://en.wikipedia.org/wiki/Mixin mixins]. When a module is included within a class,all its functionality becomes available to the class.&lt;br /&gt;
*	Modules can contain [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] and [http://www.daniweb.com/software-development/java/threads/303430/what-is-instance-method instance methods].&lt;br /&gt;
&lt;br /&gt;
==== How [http://en.wikipedia.org/wiki/Mixin mixins]is different from  #include and multiple inheritance? ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Let's see how [http://en.wikipedia.org/wiki/Mixin mixins]is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.&lt;br /&gt;
*	By using [http://en.wikipedia.org/wiki/Mixin mixins]the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Mixin mixins]corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;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;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++] (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a [http://en.wikipedia.org/wiki/Namespace namespace] is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Namespace Namespace] usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a [http://en.wikipedia.org/wiki/Namespace namespace]with the keyword [http://en.wikipedia.org/wiki/Namespace namespace].&lt;br /&gt;
*	To access the elements of an identified [http://en.wikipedia.org/wiki/Namespace namespace] by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne [http://en.wikipedia.org/wiki/Namespace namespace].  &lt;br /&gt;
*	In order to access this variables from the outside the [http://en.wikipedia.org/wiki/Namespace namespace], we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another [http://en.wikipedia.org/wiki/Namespace namespace]definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The order of derivation is relevant only to determine the order of default [http://en.wikipedia.org/wiki/Initialization_(programming) initialization] by [http://en.wikipedia.org/wiki/Constructor constructors] and cleanup by [http://en.wikipedia.org/wiki/Destructor_(computer_programming) destructors].&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A [http://en.wikipedia.org/wiki/Java_(programming_language) Java] interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the [http://en.wikipedia.org/wiki/Interface_(Java) interface] agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An [http://www.codeproject.com/Articles/10553/Using-Interfaces-in-C interface] is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an [http://en.wikipedia.org/wiki/Interface_(Java) interface], you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the [http://en.wikipedia.org/wiki/Interface_(Java) interface], the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	A class uses the [http://www.roseindia.net/help/java/i/implement-keyword.shtml implements] keyword to implement an [http://en.wikipedia.org/wiki/Interface_(Java) interface]. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable] mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	The class must define the &amp;lt;=&amp;gt; [http://en.wikipedia.org/wiki/Operator 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. &lt;br /&gt;
*	If the other object is not comparable then is should just return nil.[http://www.ruby-doc.org/core-1.9.3/Comparable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;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 [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable]mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.3/Enumerable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of [http://ruby-doc.org/core-1.9.3/Enumerable.html 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 [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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 compile time or run time.&lt;br /&gt;
*	Ruby does not support direct[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]. But, Multiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Although [http://en.wikipedia.org/wiki/Multiple_inheritance 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;
*	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/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses super-classes].&lt;br /&gt;
*	For example, in Eiffel, the library class WINDOW is a [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclass] of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses]. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses]. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Interface_(Java) Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses super-class] and the [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses sub-classes] inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, [http://www.rubyist.net/~slagell/ruby/objinitialization.html initialize()]) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during [http://en.wikipedia.org/wiki/Compile_time compile time] and can lead to [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time errors].&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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 [http://it.toolbox.com/wiki/index.php/'is_a'_relationship_and_a_'has_a'_relationship_in_Java &amp;quot;is-a&amp;quot;] relationship between the sub-class and super-class. Here, there is a [http://it.toolbox.com/wiki/index.php/'is_a'_relationship_and_a_'has_a'_relationship_in_Java &amp;quot;has-a&amp;quot;] relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Set based language - object specification	&lt;br /&gt;
! Protocol 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;
| 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;
| After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes. &lt;br /&gt;
| Multiple instances of that object are obtained by copying or cloning. &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.	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;
| 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;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* In a prototype-based object specification system, the objects are defined using a [http://en.wikipedia.org/wiki/Top-down_and_bottom-up_design bottom-up] approach by first specifying the specific instance and modifying it as necessary to represent other instances.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&lt;br /&gt;
* The [http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html include] statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* 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;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
[http://positiveincline.com/index.php/2009/06/dynamically-extending-object-behaviour-in-ruby-and-python-a-quick-warts-and-all-comparison/ Extending Objects] - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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]&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Ruby extend and Include]&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_modules.htm Ruby Multiple Inheritance with modules and mixins]&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Learn Ruby]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Perl Perl]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming Ruby: Program Like the Ruby Pros]&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&lt;br /&gt;
[http://www.rubyinside.com/how-ruby-manages-memory-and-garbage-collection-3006.html Garbage Collection in Ruby]&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html Threads in Ruby]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70566</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w45 pg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w45_pg&amp;diff=70566"/>
		<updated>2012-11-19T09:26:44Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: Created page with &amp;quot;Command Pattern  Introduction  Definition: (http://en.wikipedia.org/wiki/Command_pattern)  The command pattern is a behavioural design pattern in which an object is used to repre...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Command Pattern&lt;br /&gt;
&lt;br /&gt;
Introduction&lt;br /&gt;
&lt;br /&gt;
Definition: (http://en.wikipedia.org/wiki/Command_pattern)&lt;br /&gt;
&lt;br /&gt;
The command pattern is a behavioural design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.&lt;br /&gt;
&lt;br /&gt;
Three terms always associated with the command pattern are client, invoker and receiver. &lt;br /&gt;
&lt;br /&gt;
Client&lt;br /&gt;
The client instantiates the command object and provides the information required to call the method at a later time. &lt;br /&gt;
Invoker&lt;br /&gt;
The invoker decides when the method should be called. &lt;br /&gt;
Receiver &lt;br /&gt;
The receiver is an instance of the class that contains the method's code.&lt;br /&gt;
&lt;br /&gt;
How does it works?&lt;br /&gt;
Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.&lt;br /&gt;
 &lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
&lt;br /&gt;
Command Real world Example &lt;br /&gt;
(http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html)&lt;br /&gt;
&lt;br /&gt;
The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parameterized with different requests. The &amp;quot;check&amp;quot; at a diner is an example of a Command pattern. The waiter or waitress takes an order, or command from a customer, and encapsulates that order by writing it on the check. The order is then queued for a short order cook. Note that the pad of &amp;quot;checks&amp;quot; used by different diners is not dependent on the menu, and therefore they can support commands to cook many different items.&lt;br /&gt;
 &lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;br /&gt;
&lt;br /&gt;
When Would I Use This Pattern?&lt;br /&gt;
The Command Pattern is useful when:&lt;br /&gt;
•	A history of requests is needed&lt;br /&gt;
•	You need callback functionality&lt;br /&gt;
•	Requests need to be handled at variant times or in variant orders&lt;br /&gt;
•	The invoker should be decoupled from the object handling the invocation.&lt;br /&gt;
You'll see command being used a lot when you need to have multiple undo operations, where a stack of the recently executed commands are maintained. To implement the undo, all you need to do is get the last Command in the stack and execute it's undo() method.&lt;br /&gt;
You'll also find Command useful for wizards, progress bars, GUI buttons and menu actions, and other transactional behaviour. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Directory of sites&lt;br /&gt;
http://msdn.microsoft.com/en-us/magazine/cc163920.aspx&lt;br /&gt;
&lt;br /&gt;
Command pattern on MSDN Microsoft Website:&lt;br /&gt;
Command Pattern to -Simplify Distributed System Design Using the Command Pattern, MSMQ, and .NET &lt;br /&gt;
&lt;br /&gt;
Object-based collaboration VS service-based collaboration&lt;br /&gt;
Communication or exchange of request and service and the format or the pattern based on play an important criteria in successful implementation of  object-based collaboration and service-oriented architecture. &lt;br /&gt;
&lt;br /&gt;
Systems generally share a common platform, allowing you to optimize in ways you can't on widely dispersed -systems that span various operating systems and development environments are preferred to follow object-based collaboration than service-based collaboration.&lt;br /&gt;
&lt;br /&gt;
But generally the participants are hybrid and they don't share a common platform,  in such situations service oriented architecture comes handy. here the requests are queued in Microsoft Message Queuing (MSMQ) which implements Command design pattern. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The distributed command system discussed here is the combination of reliable messaging, services, and the Command pattern. While most often seen as a design pattern for implementing desktop application menu systems, the Command pattern offers advantages for distributed systems when combined with a robust messaging infrastructure like MSMQ. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Learning: &lt;br /&gt;
&lt;br /&gt;
The concept is very simple, in service oriented architecture, the participants don't share a common platform, hence you need a mechanism, a design pattern, a module to issue requests to objects.  &lt;br /&gt;
&lt;br /&gt;
The module that is doing the requesting doesn’t know what the receiver will be, or what operation will be requested.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Pattern and Participants&lt;br /&gt;
Class	Description&lt;br /&gt;
Command	Abstract base class for all Command objects&lt;br /&gt;
Concrete command	Any number of classes derived from Command&lt;br /&gt;
Client	The creator of a command&lt;br /&gt;
Invoker	Executes a command&lt;br /&gt;
Receiver	Any class that is acted on by a command&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example: &lt;br /&gt;
In ASP.NET, the database communication are based on the commands, the database commands such as create connection, command statement, parameter doesn't know what the object is and what the object it is requesting and these are typical example of command design pattern it best.&lt;br /&gt;
&lt;br /&gt;
Conclusion:&lt;br /&gt;
The Command pattern specifies a set of interactions that you can apply to the development of distributed systems in .NET. Use this pattern in your application to implement a high-performance, extendable backbone connecting your near link nodes that run .NET-enabled operating systems to achieve optimal performance and rapid development. Distributed commands are a mix of classic object orientation, messaging, and service architecture that together fill a definite need in the distributed systems space.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
The benefit of the Command class is that it makes it easier to add new functions to an application. &lt;br /&gt;
If we add a new capability, we no longer need to reopen the Controller class to code routing information. &lt;br /&gt;
Instead, we encapsulate the information in a Command object and assign it to whatever control we use in the View to invoke the new capability. &lt;br /&gt;
When the Command gets passed to the Controller, the Controller can use it to route the request, without knowing anything about it.&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. &lt;br /&gt;
&lt;br /&gt;
The routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. &lt;br /&gt;
&lt;br /&gt;
The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Web Application Component Toolkit (WACT)&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
&lt;br /&gt;
Web Application Component Toolkit&lt;br /&gt;
The Web Application Component Toolkit is a framework for creating web applications. WACT facilitates a modular approach where individual, independent or reusable components may be integrated into a larger web application. WACT assists in implementing the Model View Controller pattern and the related Domain Model, Template View, Front Controller and Application Controller patterns. WACT emphasizes writing secure web applications.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
WACT sees Command Pattern as&lt;br /&gt;
A GoF Design Pattern with the intent: “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”&lt;br /&gt;
&lt;br /&gt;
In WACT, this is seen with the FormController, where users can define a class which must contain the method performAction();&lt;br /&gt;
&lt;br /&gt;
class AddressForm extends PostFormController {&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
 &lt;br /&gt;
    function InitializeActions() {&lt;br /&gt;
        $this-&amp;gt;registerSubmitAction(&amp;quot;register&amp;quot;, &amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
        $this-&amp;gt;registerDefaultSubmitAction(&amp;quot;RegisterUser&amp;quot;, PRE_VALID);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    // Code omitted&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The registerSubmitAction call above points the controller at the class RegisterUser, which is where the Command pattern resides.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class RegisterUser {&lt;br /&gt;
 &lt;br /&gt;
    function performAction(&amp;amp;$context) {&lt;br /&gt;
        $DataSpace =&amp;amp; $context-&amp;gt;getDataSpace();&lt;br /&gt;
        $Page =&amp;amp; new Template('/input/thanks.html');&lt;br /&gt;
        $Page-&amp;gt;import($DataSpace-&amp;gt;export());&lt;br /&gt;
        $Page-&amp;gt;display();&lt;br /&gt;
        return FORM_COMPLETE;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The FormController expects to find the performAction method in the Action Class RegisterUser, calling it when the form is submitted (and successfully validated).&lt;br /&gt;
&lt;br /&gt;
The Command pattern is a Gang of Four Design Pattern, used to allow a function name to be wrapped up in a class namespace, so that some code, which will call the function, can be given an instance of one of many classes without caring which which instance it’s dealing with.&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
Here the Form controller in its post functionality exhibits command pattern as it requires the object that calls the post form functionality to implement performAction method, the actions that the object needs to perform when the form is submitted, but the post form controller doesn't know what are actions performed by the performAction method and whose performAction  method is called all it requires is it needs a class that has the performAction method to post it forms. &lt;br /&gt;
Hence the performAction acts like a command that is being called whenever the form is posted.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another Example:&lt;br /&gt;
PHP example &lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
/* interface */ class Tag {&lt;br /&gt;
   // The abstract command method&lt;br /&gt;
   function render($content);&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Bold {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;b&amp;gt;$content&amp;lt;/b&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
class Italic {&lt;br /&gt;
   function render($content) {&lt;br /&gt;
       return &amp;quot;&amp;lt;i&amp;gt;$content&amp;lt;/i&amp;gt;&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
$commands = array (&lt;br /&gt;
    new Bold(),new Italic(), new Italic(), new Bold()&lt;br /&gt;
);&lt;br /&gt;
 &lt;br /&gt;
// Iterate over the command objects&lt;br /&gt;
foreach ( $commands as $command ) {&lt;br /&gt;
    echo ( $command-&amp;gt;render('Hello World!') );&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the PHP example provided above, class Bold, Italic works on the function render.&lt;br /&gt;
&lt;br /&gt;
In class Tag, all it does is for each command in the command object, render functionality is called, the render doesn't know which command, bold or italics is called and what doesn't knows the functionality of command (bold or italics) &lt;br /&gt;
&lt;br /&gt;
Applications&lt;br /&gt;
&lt;br /&gt;
The Command pattern is used widely on in web based application frameworks like Java Struts from a PHP Perspective, usually in Action Classes, to allow framework users to define logic which responds to “events” in the application. Action Classes can be regarded as the “verbs” of the framework - they act on things - they do things.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Comparison to similar patterns&lt;br /&gt;
&lt;br /&gt;
Commands and Mementos have some similarity due to the fact they both work with an object's internal properties. The Command pattern keeps a record of changes to an object's state, and applies those changes in an ad-hoc fashion. A Memento also records changes to an object's state, and can restore that state at any time. The Chain of Responsibility pattern seems to handle processing in a similar manner to the Command, except it hands off processing to another process linearly unlike the Command pattern. An Interpreter pattern works in the example above because we are using language elements to determine which changes to apply at a given time.&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
Since the object that implements the command design doesn't know what function it does and what object it affects, as it acts like a black box, the implementation of the objects that is calling the black box can be changed and it can be implemented independently without affecting the flow of the program.&lt;br /&gt;
&lt;br /&gt;
Hence command pattern gives a wide range of flexibility and sophisticated design features.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Disadvantages:&lt;br /&gt;
&lt;br /&gt;
It complicates the application design. Every pattern complicates application design; they’re used because the benefits outweigh the cost. In fact, they should only be used when the benefits outweigh the costs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command Pattern on Javaworld&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
&lt;br /&gt;
According to Javaworld,&lt;br /&gt;
&lt;br /&gt;
The design patterns not only accelerate the design phase of an object-oriented (OO) project but also increase the productivity of the development team and quality of the software. &lt;br /&gt;
&lt;br /&gt;
They describe a Command pattern as an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. &lt;br /&gt;
&lt;br /&gt;
Sender -  invokes an operation&lt;br /&gt;
Receiver - receives the request to execute a certain operation. &lt;br /&gt;
Decoupling - the sender has no knowledge of the Receiver's interface&lt;br /&gt;
Request  - the command that is to be executed. &lt;br /&gt;
&lt;br /&gt;
Command pattern provides us flexibility as well as extensibility.&lt;br /&gt;
&lt;br /&gt;
The program discussed in the javaworld web directory is class Fan {&lt;br /&gt;
        public void startRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void stopRotate() {&lt;br /&gt;
                System.out.println(&amp;quot;Fan is not rotating&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Light {&lt;br /&gt;
        public void turnOn( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is on &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        public void turnOff( ) {&lt;br /&gt;
                System.out.println(&amp;quot;Light is off&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class Switch {&lt;br /&gt;
        private Command UpCommand, DownCommand;&lt;br /&gt;
        public Switch( Command Up, Command Down) {&lt;br /&gt;
                UpCommand = Up; // concrete Command registers itself with the invoker &lt;br /&gt;
                DownCommand = Down;&lt;br /&gt;
        }&lt;br /&gt;
        void flipUp( ) { // invoker calls back concrete Command, which executes the Command on the receiver &lt;br /&gt;
                        UpCommand . execute ( ) ;                           &lt;br /&gt;
        }&lt;br /&gt;
        void flipDown( ) {&lt;br /&gt;
                        DownCommand . execute ( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOnCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOnCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOn( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class LightOffCommand implements Command {&lt;br /&gt;
        private Light myLight;&lt;br /&gt;
        public LightOffCommand ( Light L) {&lt;br /&gt;
                myLight  =  L;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myLight . turnOff( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOnCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOnCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . startRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
class FanOffCommand implements Command {&lt;br /&gt;
        private Fan myFan;&lt;br /&gt;
        public FanOffCommand ( Fan F) {&lt;br /&gt;
                myFan  =  F;&lt;br /&gt;
        }&lt;br /&gt;
        public void execute( ) {&lt;br /&gt;
                myFan . stopRotate( );&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
public class TestCommand {&lt;br /&gt;
                public static void main(String[] args) {&lt;br /&gt;
                        Light  testLight = new Light( );&lt;br /&gt;
                        LightOnCommand testLOC = new LightOnCommand(testLight);&lt;br /&gt;
                        LightOffCommand testLFC = new LightOffCommand(testLight);&lt;br /&gt;
                        Switch testSwitch = new Switch( testLOC,testLFC);       &lt;br /&gt;
                        testSwitch.flipUp( );&lt;br /&gt;
                        testSwitch.flipDown( );&lt;br /&gt;
                        Fan testFan = new Fan( );&lt;br /&gt;
                        FanOnCommand foc = new FanOnCommand(testFan);&lt;br /&gt;
                        FanOffCommand ffc = new FanOffCommand(testFan);&lt;br /&gt;
                        Switch ts = new Switch( foc,ffc);&lt;br /&gt;
                        ts.flipUp( );&lt;br /&gt;
                        ts.flipDown( ); &lt;br /&gt;
                }&lt;br /&gt;
}               &lt;br /&gt;
Command.java&lt;br /&gt;
public interface Command {&lt;br /&gt;
        public abstract void execute ( );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Learning:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the above example,&lt;br /&gt;
The class Fan has startRotate() and stopRotate() methods,the class Light has turnOn() and turnOff() methods.&lt;br /&gt;
The catch here is startRotate of Fan and turnOn of Light is performed when the switch is turned on and stopRotate of Fan and turnOff of Light is performed when the switch in turned off.&lt;br /&gt;
&lt;br /&gt;
Keeping this real world catch, the class switch is created as a kind of interface for &lt;br /&gt;
fan and light to work on it turn on and turn off requests.&lt;br /&gt;
&lt;br /&gt;
The switch has flipUp and flipDown methods which acts like command design implementation.&lt;br /&gt;
The switch doesn't know what action it performs on flipUp and flipDown and what object is affected when flipUp and flipDown is pressed. &lt;br /&gt;
&lt;br /&gt;
Hence flipUp and flipDown acts like a command without knowing what function it does and what object it affects, but eventually performing the required action. That is the beauty of command design. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Command pattern has the following advantages:&lt;br /&gt;
1.	A command decouples the object that invokes the operation from the one that knows how to perform it.&lt;br /&gt;
2.	A command is a first-class object. It can be manipulated and extended like any other object.&lt;br /&gt;
3.	Commands can be assembled into a composite command.&lt;br /&gt;
4.	It's easy to add new commands, because you don't have to change the existing classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References&lt;br /&gt;
http://www.itsmedia.ca/the-command-pattern-and-restful-interfaces/&lt;br /&gt;
http://i.msdn.microsoft.com/cc163920.fig02(en-us).gif&lt;br /&gt;
http://www.codeproject.com/Articles/12263/The-Command-Pattern-and-MVC-Architecture&lt;br /&gt;
http://www.phpwact.org/&lt;br /&gt;
http://www.phpwact.org/pattern/command&lt;br /&gt;
http://www.javaworld.com/javatips/jw-javatip68.html?page=1&lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
http://upload.wikimedia.org/wikipedia/commons/8/8e/Command_Design_Pattern_Class_Diagram.png&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/patexamples.html&lt;br /&gt;
http://www.cours.polymtl.ca/inf3700/divers/nonSoftwareExample/pateximg14.gif&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=70565</id>
		<title>CSC/ECE 517 Fall 2012</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=70565"/>
		<updated>2012-11-19T09:26:24Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE_517_Fall_2012/Table_Of_Contents]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 n xx]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w1 rk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w20 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w5 su]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w6 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w4 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w7 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w8 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w9 av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w10 pk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w11 ap]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w12 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w14 gv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w17 ir]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w22 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 wi]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w31 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w16 br]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w23 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w24 nr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w15 rt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w3 pl]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w32 cm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w5 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w37 ss]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w67 ks]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w27 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w29 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w33 op]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w19 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w34 vd]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w35 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w30 rp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w58 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w47 sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w69 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w44 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w45 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w53 kc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 ar]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w39 sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w54 go]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w56 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w64 nn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w66 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w42 js]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w46 sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w71 gs]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w63 dv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w55 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w57 mp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w52 an]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch1b 1w38 nm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w60 ac]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w62 rb]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w29 st]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w3_sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w30 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w17 pt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w31 up]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w9 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w19 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w26 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w5 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w16 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w8 vp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w3 jm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w23 sr]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w11_aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w15 rr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w33 pv]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w20_aa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w14_bb]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w21_ap]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w13_sm]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w4_sa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w25_nr]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w12_sv]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w7_ma]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w6_ar]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w32_mk]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w10_rc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w70_sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w67_sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w40_sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w22_sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w-1w65_am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w59_bc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w60_ns]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b 2w47 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w69_as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w39_ka]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w36_av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w37_ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w43_iv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w53_iv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w63_sp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w49_ps]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w52_sj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w28_dh]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 2w41 dc]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2b_1w59_nm]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2b_1w61_ps]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2b_2w57]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2b_2w42_aa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2b_1w61_ns]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2b_2w51_aa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2b_2w45_pg]]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=70564</id>
		<title>CSC/ECE 517 Fall 2012</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=70564"/>
		<updated>2012-11-19T09:24:21Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: Undo revision 70563 by Psivash2 (talk)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE_517_Fall_2012/Table_Of_Contents]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 n xx]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w1 rk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w20 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w5 su]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w6 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w4 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w7 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w8 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w9 av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w10 pk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w11 ap]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w12 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w14 gv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w17 ir]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w22 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 wi]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w31 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w16 br]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w23 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w24 nr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w15 rt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w3 pl]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w32 cm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w5 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w37 ss]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w67 ks]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w27 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w29 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w33 op]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w19 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w34 vd]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w35 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w30 rp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w58 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w47 sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w69 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w44 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w45 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w53 kc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 ar]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w39 sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w54 go]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w56 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w64 nn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w66 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w42 js]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w46 sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w71 gs]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w63 dv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w55 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w57 mp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w52 an]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch1b 1w38 nm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w60 ac]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w62 rb]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w29 st]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w3_sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w30 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w17 pt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w31 up]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w9 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w19 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w26 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w5 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w16 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w8 vp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w3 jm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w23 sr]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w11_aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w15 rr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w33 pv]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w20_aa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w14_bb]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w21_ap]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w13_sm]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w4_sa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w25_nr]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w12_sv]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w7_ma]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w6_ar]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w32_mk]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w10_rc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w70_sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w67_sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w40_sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w22_sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w-1w65_am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w59_bc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w60_ns]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b 2w47 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w69_as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w39_ka]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w36_av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w37_ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w43_iv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w53_iv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w63_sp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w49_ps]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w52_sj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w28_dh]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 2w41 dc]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2b_1w59_nm]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2b_1w61_ps]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2b_2w57]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2b_2w42_aa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2b_1w61_ns]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2b_2w51_aa]]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=70563</id>
		<title>CSC/ECE 517 Fall 2012</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=70563"/>
		<updated>2012-11-19T09:23:45Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: Replaced content with &amp;quot; Gopi&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt; Gopi&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67367</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67367"/>
		<updated>2012-10-10T10:10:51Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
[[File:1.jpg]]&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby [http://en.wikipedia.org/wiki/Mixin Modules] are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Unlike [http://en.wikipedia.org/wiki/Object-oriented_programming classes], [http://en.wikipedia.org/wiki/Object-oriented_programming 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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &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.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby [http://ruby.about.com/od/rubyfeatures/a/require.htm require] statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The most interesting fact about the use of modules is to define [http://en.wikipedia.org/wiki/Mixin mixins]. When a module is included within a class,all its functionality becomes available to the class.&lt;br /&gt;
*	Modules can contain [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] and [http://www.daniweb.com/software-development/java/threads/303430/what-is-instance-method instance methods].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How [http://en.wikipedia.org/wiki/Mixin mixins]is different from  #include and multiple inheritance? ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Let's see how [http://en.wikipedia.org/wiki/Mixin mixins]is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.&lt;br /&gt;
*	By using [http://en.wikipedia.org/wiki/Mixin mixins]the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Mixin mixins]corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;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;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++] (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a [http://en.wikipedia.org/wiki/Namespace namespace] is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Namespace Namespace] usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a [http://en.wikipedia.org/wiki/Namespace namespace]with the keyword [http://en.wikipedia.org/wiki/Namespace namespace].&lt;br /&gt;
*	To access the elements of an identified [http://en.wikipedia.org/wiki/Namespace namespace] by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne [http://en.wikipedia.org/wiki/Namespace namespace].  &lt;br /&gt;
*	In order to access this variables from the outside the [http://en.wikipedia.org/wiki/Namespace namespace], we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another [http://en.wikipedia.org/wiki/Namespace namespace]definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The order of derivation is relevant only to determine the order of default [http://en.wikipedia.org/wiki/Initialization_(programming) initialization] by [http://en.wikipedia.org/wiki/Constructor constructors] and cleanup by [http://en.wikipedia.org/wiki/Destructor_(computer_programming) destructors].&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A [http://en.wikipedia.org/wiki/Java_(programming_language) Java] interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the [http://en.wikipedia.org/wiki/Interface_(Java) interface] agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An [http://www.codeproject.com/Articles/10553/Using-Interfaces-in-C interface] is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an [http://en.wikipedia.org/wiki/Interface_(Java) interface], you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the [http://en.wikipedia.org/wiki/Interface_(Java) interface], the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	A class uses the [http://www.roseindia.net/help/java/i/implement-keyword.shtml implements] keyword to implement an [http://en.wikipedia.org/wiki/Interface_(Java) interface]. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable] mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	The class must define the &amp;lt;=&amp;gt; [http://en.wikipedia.org/wiki/Operator 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. &lt;br /&gt;
*	If the other object is not comparable then is should just return nil.[http://www.ruby-doc.org/core-1.9.3/Comparable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;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 [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable]mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.3/Enumerable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of [http://ruby-doc.org/core-1.9.3/Enumerable.html 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 [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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 compile time or run time.&lt;br /&gt;
*	Ruby does not support direct[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]. But, Multiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Although [http://en.wikipedia.org/wiki/Multiple_inheritance 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;
*	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/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses super-classes].&lt;br /&gt;
*	For example, in Eiffel, the library class WINDOW is a [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclass] of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses]. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses]. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Interface_(Java) Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses super-class] and the [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses sub-classes] inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, [http://www.rubyist.net/~slagell/ruby/objinitialization.html initialize()]) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during [http://en.wikipedia.org/wiki/Compile_time compile time] and can lead to [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time errors].&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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 [http://it.toolbox.com/wiki/index.php/'is_a'_relationship_and_a_'has_a'_relationship_in_Java &amp;quot;is-a&amp;quot;] relationship between the sub-class and super-class. Here, there is a [http://it.toolbox.com/wiki/index.php/'is_a'_relationship_and_a_'has_a'_relationship_in_Java &amp;quot;has-a&amp;quot;] relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Set based language - object specification	&lt;br /&gt;
! Protocol 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;
| 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;
| After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes. &lt;br /&gt;
| Multiple instances of that object are obtained by copying or cloning. &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.	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;
| 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;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* In a prototype-based object specification system, the objects are defined using a [http://en.wikipedia.org/wiki/Top-down_and_bottom-up_design bottom-up] approach by first specifying the specific instance and modifying it as necessary to represent other instances.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&lt;br /&gt;
* The [http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html include] statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* 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;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
[http://positiveincline.com/index.php/2009/06/dynamically-extending-object-behaviour-in-ruby-and-python-a-quick-warts-and-all-comparison/ Extending Objects] - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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]&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Ruby extend and Include]&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_modules.htm Ruby Multiple Inheritance with modules and mixins]&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Learn Ruby]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Perl Perl]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming Ruby: Program Like the Ruby Pros]&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&lt;br /&gt;
[http://www.rubyinside.com/how-ruby-manages-memory-and-garbage-collection-3006.html Garbage Collection in Ruby]&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html Threads in Ruby]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67366</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67366"/>
		<updated>2012-10-10T10:03:47Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby [http://en.wikipedia.org/wiki/Mixin Modules] are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Unlike [http://en.wikipedia.org/wiki/Object-oriented_programming classes], [http://en.wikipedia.org/wiki/Object-oriented_programming 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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &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.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby [http://ruby.about.com/od/rubyfeatures/a/require.htm require] statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The most interesting fact about the use of modules is to define [http://en.wikipedia.org/wiki/Mixin mixins]. When a module is included within a class,all its functionality becomes available to the class.&lt;br /&gt;
*	Modules can contain [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] and [http://www.daniweb.com/software-development/java/threads/303430/what-is-instance-method instance methods].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How [http://en.wikipedia.org/wiki/Mixin mixins]is different from  #include and multiple inheritance? ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Let's see how [http://en.wikipedia.org/wiki/Mixin mixins]is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.&lt;br /&gt;
*	By using [http://en.wikipedia.org/wiki/Mixin mixins]the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Mixin mixins]corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;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;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++] (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a [http://en.wikipedia.org/wiki/Namespace namespace] is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Namespace Namespace] usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a [http://en.wikipedia.org/wiki/Namespace namespace]with the keyword [http://en.wikipedia.org/wiki/Namespace namespace].&lt;br /&gt;
*	To access the elements of an identified [http://en.wikipedia.org/wiki/Namespace namespace] by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne [http://en.wikipedia.org/wiki/Namespace namespace].  &lt;br /&gt;
*	In order to access this variables from the outside the [http://en.wikipedia.org/wiki/Namespace namespace], we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another [http://en.wikipedia.org/wiki/Namespace namespace]definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The order of derivation is relevant only to determine the order of default [http://en.wikipedia.org/wiki/Initialization_(programming) initialization] by [http://en.wikipedia.org/wiki/Constructor constructors] and cleanup by [http://en.wikipedia.org/wiki/Destructor_(computer_programming) destructors].&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A [http://en.wikipedia.org/wiki/Java_(programming_language) Java] interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the [http://en.wikipedia.org/wiki/Interface_(Java) interface] agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An [http://www.codeproject.com/Articles/10553/Using-Interfaces-in-C interface] is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an [http://en.wikipedia.org/wiki/Interface_(Java) interface], you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the [http://en.wikipedia.org/wiki/Interface_(Java) interface], the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	A class uses the [http://www.roseindia.net/help/java/i/implement-keyword.shtml implements] keyword to implement an [http://en.wikipedia.org/wiki/Interface_(Java) interface]. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable] mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	The class must define the &amp;lt;=&amp;gt; [http://en.wikipedia.org/wiki/Operator 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. &lt;br /&gt;
*	If the other object is not comparable then is should just return nil.[http://www.ruby-doc.org/core-1.9.3/Comparable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;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 [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable]mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.3/Enumerable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of [http://ruby-doc.org/core-1.9.3/Enumerable.html 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 [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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 compile time or run time.&lt;br /&gt;
*	Ruby does not support direct[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]. But, Multiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Although [http://en.wikipedia.org/wiki/Multiple_inheritance 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;
*	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/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses super-classes].&lt;br /&gt;
*	For example, in Eiffel, the library class WINDOW is a [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclass] of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses]. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses]. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Interface_(Java) Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses super-class] and the [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses sub-classes] inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, [http://www.rubyist.net/~slagell/ruby/objinitialization.html initialize()]) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during [http://en.wikipedia.org/wiki/Compile_time compile time] and can lead to [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time errors].&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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 [http://it.toolbox.com/wiki/index.php/'is_a'_relationship_and_a_'has_a'_relationship_in_Java &amp;quot;is-a&amp;quot;] relationship between the sub-class and super-class. Here, there is a [http://it.toolbox.com/wiki/index.php/'is_a'_relationship_and_a_'has_a'_relationship_in_Java &amp;quot;has-a&amp;quot;] relation and not &amp;quot;is-a&amp;quot; relationship.&lt;br /&gt;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Set based language - object specification	&lt;br /&gt;
! Protocol 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;
| 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;
| After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes. &lt;br /&gt;
| Multiple instances of that object are obtained by copying or cloning. &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.	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;
| 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;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* In a prototype-based object specification system, the objects are defined using a [http://en.wikipedia.org/wiki/Top-down_and_bottom-up_design bottom-up] approach by first specifying the specific instance and modifying it as necessary to represent other instances.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&lt;br /&gt;
* The [http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html include] statement can be used to augment the class definition and add more functionality to the class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* 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;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
[http://positiveincline.com/index.php/2009/06/dynamically-extending-object-behaviour-in-ruby-and-python-a-quick-warts-and-all-comparison/ Extending Objects] - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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]&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Ruby extend and Include]&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_modules.htm Ruby Multiple Inheritance with modules and mixins]&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Learn Ruby]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Perl Perl]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming Ruby: Program Like the Ruby Pros]&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&lt;br /&gt;
[http://www.rubyinside.com/how-ruby-manages-memory-and-garbage-collection-3006.html Garbage Collection in Ruby]&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html Threads in Ruby]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67365</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67365"/>
		<updated>2012-10-10T09:53:58Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby [http://en.wikipedia.org/wiki/Mixin Modules] are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Unlike [http://en.wikipedia.org/wiki/Object-oriented_programming classes], [http://en.wikipedia.org/wiki/Object-oriented_programming 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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &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.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby [http://ruby.about.com/od/rubyfeatures/a/require.htm require] statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The most interesting fact about the use of modules is to define [http://en.wikipedia.org/wiki/Mixin mixins]. When a module is included within a class,all its functionality becomes available to the class.&lt;br /&gt;
*	Modules can contain [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] and [http://www.daniweb.com/software-development/java/threads/303430/what-is-instance-method instance methods].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How [http://en.wikipedia.org/wiki/Mixin mixins]is different from  #include and multiple inheritance? ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Let's see how [http://en.wikipedia.org/wiki/Mixin mixins]is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.&lt;br /&gt;
*	By using [http://en.wikipedia.org/wiki/Mixin mixins]the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Mixin mixins]corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;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;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++] (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a [http://en.wikipedia.org/wiki/Namespace namespace] is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Namespace Namespace] usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a [http://en.wikipedia.org/wiki/Namespace namespace]with the keyword [http://en.wikipedia.org/wiki/Namespace namespace].&lt;br /&gt;
*	To access the elements of an identified [http://en.wikipedia.org/wiki/Namespace namespace] by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne [http://en.wikipedia.org/wiki/Namespace namespace].  &lt;br /&gt;
*	In order to access this variables from the outside the [http://en.wikipedia.org/wiki/Namespace namespace], we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another [http://en.wikipedia.org/wiki/Namespace namespace]definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The order of derivation is relevant only to determine the order of default [http://en.wikipedia.org/wiki/Initialization_(programming) initialization] by [http://en.wikipedia.org/wiki/Constructor constructors] and cleanup by [http://en.wikipedia.org/wiki/Destructor_(computer_programming) destructors].&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A [http://en.wikipedia.org/wiki/Java_(programming_language) Java] interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the [http://en.wikipedia.org/wiki/Interface_(Java) interface] agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An [http://www.codeproject.com/Articles/10553/Using-Interfaces-in-C interface] is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an [http://en.wikipedia.org/wiki/Interface_(Java) interface], you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the [http://en.wikipedia.org/wiki/Interface_(Java) interface], the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	A class uses the [http://www.roseindia.net/help/java/i/implement-keyword.shtml implements] keyword to implement an [http://en.wikipedia.org/wiki/Interface_(Java) interface]. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable] mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	The class must define the &amp;lt;=&amp;gt; [http://en.wikipedia.org/wiki/Operator 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. &lt;br /&gt;
*	If the other object is not comparable then is should just return nil.[http://www.ruby-doc.org/core-1.9.3/Comparable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;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 [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable]mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.3/Enumerable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of [http://ruby-doc.org/core-1.9.3/Enumerable.html 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 [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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 compile time or run time.&lt;br /&gt;
*	Ruby does not support direct[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]. But, Multiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Although [http://en.wikipedia.org/wiki/Multiple_inheritance 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;
*	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/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses super-classes].&lt;br /&gt;
*	For example, in Eiffel, the library class WINDOW is a [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclass] of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses]. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses]. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Interface_(Java) Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses super-class] and the [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses sub-classes] inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Set based language - object specification	&lt;br /&gt;
! Protocol 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;
| 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;
| After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes. &lt;br /&gt;
| Multiple instances of that object are obtained by copying or cloning. &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.	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;
| 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;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* 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;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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]&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Ruby extend and Include]&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_modules.htm Ruby Multiple Inheritance with modules and mixins]&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Learn Ruby]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Perl Perl]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming Ruby: Program Like the Ruby Pros]&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&lt;br /&gt;
[http://www.rubyinside.com/how-ruby-manages-memory-and-garbage-collection-3006.html Garbage Collection in Ruby]&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html Threads in Ruby]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67364</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67364"/>
		<updated>2012-10-10T09:51:32Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby [http://en.wikipedia.org/wiki/Mixin Modules] are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Unlike [http://en.wikipedia.org/wiki/Object-oriented_programming classes], [http://en.wikipedia.org/wiki/Object-oriented_programming 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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &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.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby [http://ruby.about.com/od/rubyfeatures/a/require.htm require] statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The most interesting fact about the use of modules is to define [http://en.wikipedia.org/wiki/Mixin mixins]. When a module is included within a class,all its functionality becomes available to the class.&lt;br /&gt;
*	Modules can contain [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] and [http://www.daniweb.com/software-development/java/threads/303430/what-is-instance-method instance methods].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How [http://en.wikipedia.org/wiki/Mixin mixins]is different from  #include and multiple inheritance? ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Let's see how [http://en.wikipedia.org/wiki/Mixin mixins]is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.&lt;br /&gt;
*	By using [http://en.wikipedia.org/wiki/Mixin mixins]the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Mixin mixins]corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;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;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++] (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a [http://en.wikipedia.org/wiki/Namespace namespace] is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Namespace Namespace] usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a [http://en.wikipedia.org/wiki/Namespace namespace]with the keyword [http://en.wikipedia.org/wiki/Namespace namespace].&lt;br /&gt;
*	To access the elements of an identified [http://en.wikipedia.org/wiki/Namespace namespace] by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne [http://en.wikipedia.org/wiki/Namespace namespace].  &lt;br /&gt;
*	In order to access this variables from the outside the [http://en.wikipedia.org/wiki/Namespace namespace], we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another [http://en.wikipedia.org/wiki/Namespace namespace]definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
[[File:C:\Users\Gopi\Desktop\OOLS_WIKI\1.gif]]&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The order of derivation is relevant only to determine the order of default [http://en.wikipedia.org/wiki/Initialization_(programming) initialization] by [http://en.wikipedia.org/wiki/Constructor constructors] and cleanup by [http://en.wikipedia.org/wiki/Destructor_(computer_programming) destructors].&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A [http://en.wikipedia.org/wiki/Java_(programming_language) Java] interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the [http://en.wikipedia.org/wiki/Interface_(Java) interface] agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An [http://www.codeproject.com/Articles/10553/Using-Interfaces-in-C interface] is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an [http://en.wikipedia.org/wiki/Interface_(Java) interface], you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the [http://en.wikipedia.org/wiki/Interface_(Java) interface], the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	A class uses the [http://www.roseindia.net/help/java/i/implement-keyword.shtml implements] keyword to implement an [http://en.wikipedia.org/wiki/Interface_(Java) interface]. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable] mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	The class must define the &amp;lt;=&amp;gt; [http://en.wikipedia.org/wiki/Operator 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. &lt;br /&gt;
*	If the other object is not comparable then is should just return nil.[http://www.ruby-doc.org/core-1.9.3/Comparable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;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 [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable]mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.3/Enumerable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of [http://ruby-doc.org/core-1.9.3/Enumerable.html 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 [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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 compile time or run time.&lt;br /&gt;
*	Ruby does not support direct[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]. But, Multiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Although [http://en.wikipedia.org/wiki/Multiple_inheritance 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;
*	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/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses super-classes].&lt;br /&gt;
*	For example, in Eiffel, the library class WINDOW is a [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclass] of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses]. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses]. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Interface_(Java) Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses super-class] and the [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses sub-classes] inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Set based language - object specification	&lt;br /&gt;
! Protocol 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;
| 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;
| After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes. &lt;br /&gt;
| Multiple instances of that object are obtained by copying or cloning. &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.	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;
| 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;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* 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;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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]&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Ruby extend and Include]&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_modules.htm Ruby Multiple Inheritance with modules and mixins]&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Learn Ruby]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Perl Perl]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming Ruby: Program Like the Ruby Pros]&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&lt;br /&gt;
[http://www.rubyinside.com/how-ruby-manages-memory-and-garbage-collection-3006.html Garbage Collection in Ruby]&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html Threads in Ruby]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67363</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67363"/>
		<updated>2012-10-10T09:50:33Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby [http://en.wikipedia.org/wiki/Mixin Modules] are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Unlike [http://en.wikipedia.org/wiki/Object-oriented_programming classes], [http://en.wikipedia.org/wiki/Object-oriented_programming 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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
2.Modules allow to share functionality between classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &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.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby [http://ruby.about.com/od/rubyfeatures/a/require.htm require] statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The most interesting fact about the use of modules is to define [http://en.wikipedia.org/wiki/Mixin mixins]. When a module is included within a class,all its functionality becomes available to the class.&lt;br /&gt;
*	Modules can contain [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] and [http://www.daniweb.com/software-development/java/threads/303430/what-is-instance-method instance methods].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How [http://en.wikipedia.org/wiki/Mixin mixins]is different from  #include and multiple inheritance? ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Let's see how [http://en.wikipedia.org/wiki/Mixin mixins]is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.&lt;br /&gt;
*	By using [http://en.wikipedia.org/wiki/Mixin mixins]the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Mixin mixins]corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;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;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++] (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a [http://en.wikipedia.org/wiki/Namespace namespace] is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Namespace Namespace] usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a [http://en.wikipedia.org/wiki/Namespace namespace]with the keyword [http://en.wikipedia.org/wiki/Namespace namespace].&lt;br /&gt;
*	To access the elements of an identified [http://en.wikipedia.org/wiki/Namespace namespace] by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne [http://en.wikipedia.org/wiki/Namespace namespace].  &lt;br /&gt;
*	In order to access this variables from the outside the [http://en.wikipedia.org/wiki/Namespace namespace], we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another [http://en.wikipedia.org/wiki/Namespace namespace]definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
 [[File:C:\Users\Gopi\Desktop\OOLS_WIKI\1.gif]]&lt;br /&gt;
&lt;br /&gt;
The order of derivation is relevant only to determine the order of default [http://en.wikipedia.org/wiki/Initialization_(programming) initialization] by [http://en.wikipedia.org/wiki/Constructor constructors] and cleanup by [http://en.wikipedia.org/wiki/Destructor_(computer_programming) destructors].&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A [http://en.wikipedia.org/wiki/Java_(programming_language) Java] interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the [http://en.wikipedia.org/wiki/Interface_(Java) interface] agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An [http://www.codeproject.com/Articles/10553/Using-Interfaces-in-C interface] is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an [http://en.wikipedia.org/wiki/Interface_(Java) interface], you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the [http://en.wikipedia.org/wiki/Interface_(Java) interface], the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	A class uses the [http://www.roseindia.net/help/java/i/implement-keyword.shtml implements] keyword to implement an [http://en.wikipedia.org/wiki/Interface_(Java) interface]. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable] mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	The class must define the &amp;lt;=&amp;gt; [http://en.wikipedia.org/wiki/Operator 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. &lt;br /&gt;
*	If the other object is not comparable then is should just return nil.[http://www.ruby-doc.org/core-1.9.3/Comparable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;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 [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable]mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.3/Enumerable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of [http://ruby-doc.org/core-1.9.3/Enumerable.html 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 [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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 compile time or run time.&lt;br /&gt;
*	Ruby does not support direct[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]. But, Multiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Although [http://en.wikipedia.org/wiki/Multiple_inheritance 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;
*	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/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses super-classes].&lt;br /&gt;
*	For example, in Eiffel, the library class WINDOW is a [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclass] of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses]. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses]. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Interface_(Java) Interfaces] are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses super-class] and the [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses sub-classes] inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Set based language - object specification	&lt;br /&gt;
! Protocol 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;
| 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;
| After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes. &lt;br /&gt;
| Multiple instances of that object are obtained by copying or cloning. &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.	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;
| 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;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* 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;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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]&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Ruby extend and Include]&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_modules.htm Ruby Multiple Inheritance with modules and mixins]&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Learn Ruby]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Perl Perl]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming Ruby: Program Like the Ruby Pros]&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&lt;br /&gt;
[http://www.rubyinside.com/how-ruby-manages-memory-and-garbage-collection-3006.html Garbage Collection in Ruby]&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html Threads in Ruby]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67362</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67362"/>
		<updated>2012-10-10T09:38:52Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby [http://en.wikipedia.org/wiki/Mixin Modules] are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &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.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The most interesting fact about the use of modules is to define [http://en.wikipedia.org/wiki/Mixin mixins]. When a module is included within a class,all its functionality becomes available to the class.&lt;br /&gt;
*	Modules can contain [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] and [http://www.daniweb.com/software-development/java/threads/303430/what-is-instance-method instance methods].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How [http://en.wikipedia.org/wiki/Mixin mixins]is different from  #include and multiple inheritance? ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Let's see how [http://en.wikipedia.org/wiki/Mixin mixins]is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.&lt;br /&gt;
*	By using [http://en.wikipedia.org/wiki/Mixin mixins]the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Mixin mixins]corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;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;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++] (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a [http://en.wikipedia.org/wiki/Namespace namespace] is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Namespace Namespace] usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a [http://en.wikipedia.org/wiki/Namespace namespace]with the keyword [http://en.wikipedia.org/wiki/Namespace namespace].&lt;br /&gt;
*	To access the elements of an identified [http://en.wikipedia.org/wiki/Namespace namespace] by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne [http://en.wikipedia.org/wiki/Namespace namespace].  &lt;br /&gt;
*	In order to access this variables from the outside the [http://en.wikipedia.org/wiki/Namespace namespace], we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another [http://en.wikipedia.org/wiki/Namespace namespace]definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by [http://en.wikipedia.org/wiki/Constructor constructors] and cleanup by [http://en.wikipedia.org/wiki/Destructor_(computer_programming) destructors].&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the [http://en.wikipedia.org/wiki/Interface_(Java) interface] agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An [http://www.codeproject.com/Articles/10553/Using-Interfaces-in-C interface] is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an [http://en.wikipedia.org/wiki/Interface_(Java) interface], you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the [http://en.wikipedia.org/wiki/Interface_(Java) interface], the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	Aclass uses the implements keyword to implement an [http://en.wikipedia.org/wiki/Interface_(Java) interface]. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable] mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	The class must define the &amp;lt;=&amp;gt; [http://en.wikipedia.org/wiki/Operator 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. &lt;br /&gt;
*	If the other object is not comparable then is should just return nil.[http://www.ruby-doc.org/core-1.9.3/Comparable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;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 [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable]mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.3/Enumerable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of [http://ruby-doc.org/core-1.9.3/Enumerable.html 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 [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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 compile time or run time.&lt;br /&gt;
*	Ruby does not support direct[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]. But, Multiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
*	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/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses super-classes].&lt;br /&gt;
*	For example, in Eiffel, the library class WINDOW is a [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclass] of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	Interfaces are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses super-class] and the [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses sub-classes] inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Set based language - object specification	&lt;br /&gt;
! Protocol 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;
| 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;
| After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes. &lt;br /&gt;
| Multiple instances of that object are obtained by copying or cloning. &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.	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;
| 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;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* 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;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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]&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Ruby extend and Include]&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_modules.htm Ruby Multiple Inheritance with modules and mixins]&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Learn Ruby]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Perl Perl]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming Ruby: Program Like the Ruby Pros]&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&lt;br /&gt;
[http://www.rubyinside.com/how-ruby-manages-memory-and-garbage-collection-3006.html Garbage Collection in Ruby]&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html Threads in Ruby]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67361</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67361"/>
		<updated>2012-10-10T09:36:22Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby [http://en.wikipedia.org/wiki/Mixin Modules] are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &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.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The most interesting fact about the use of modules is to define [http://en.wikipedia.org/wiki/Mixin mixins]. When a module is included within a class,all its functionality becomes available to the class.&lt;br /&gt;
*	Modules can contain [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] and [http://www.daniweb.com/software-development/java/threads/303430/what-is-instance-method instance methods].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How [http://en.wikipedia.org/wiki/Mixin mixins]is different from  #include and multiple inheritance? ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Let's see how [http://en.wikipedia.org/wiki/Mixin mixins]is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.&lt;br /&gt;
*	By using [http://en.wikipedia.org/wiki/Mixin mixins]the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Mixin mixins]corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;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;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++] (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a [http://en.wikipedia.org/wiki/Namespace namespace] is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Namespace Namespace] usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a [http://en.wikipedia.org/wiki/Namespace namespace]with the keyword [http://en.wikipedia.org/wiki/Namespace namespace].&lt;br /&gt;
*	To access the elements of an identified [http://en.wikipedia.org/wiki/Namespace namespace] by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne [http://en.wikipedia.org/wiki/Namespace namespace].  &lt;br /&gt;
*	In order to access this variables from the outside the [http://en.wikipedia.org/wiki/Namespace namespace], we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another [http://en.wikipedia.org/wiki/Namespace namespace]definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by [http://en.wikipedia.org/wiki/Constructor constructors] and cleanup by [http://en.wikipedia.org/wiki/Destructor_(computer_programming) destructors].&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the [http://en.wikipedia.org/wiki/Interface_(Java) interface] agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An [http://www.codeproject.com/Articles/10553/Using-Interfaces-in-C interface] is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an [http://en.wikipedia.org/wiki/Interface_(Java) interface], you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the [http://en.wikipedia.org/wiki/Interface_(Java) interface], the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	Aclass uses the implements keyword to implement an [http://en.wikipedia.org/wiki/Interface_(Java) interface]. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable] mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	The class must define the &amp;lt;=&amp;gt; [http://en.wikipedia.org/wiki/Operator 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. &lt;br /&gt;
*	If the other object is not comparable then is should just return nil.[http://www.ruby-doc.org/core-1.9.3/Comparable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;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 [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable]mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.3/Enumerable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of [http://ruby-doc.org/core-1.9.3/Enumerable.html 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 [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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 compile time or run time.&lt;br /&gt;
*	Ruby does not support direct[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance]. But, Multiple Inheritance can be achieved in Ruby through Modules. Modules simulate multiple inheritance in Ruby.&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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
*	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/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses super-classes].&lt;br /&gt;
*	For example, in Eiffel, the library class WINDOW is a [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclass] of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	Interfaces are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses super-class] and the [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses sub-classes] inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 === Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Set based language - object specification	&lt;br /&gt;
! Protocol 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;
| 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;
| After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes. &lt;br /&gt;
| Multiple instances of that object are obtained by copying or cloning. &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.	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;
| 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;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* 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;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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]&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Ruby extend and Include]&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_modules.htm Ruby Multiple Inheritance with modules and mixins]&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Learn Ruby]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Perl Perl]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming Ruby: Program Like the Ruby Pros]&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&lt;br /&gt;
[http://www.rubyinside.com/how-ruby-manages-memory-and-garbage-collection-3006.html Garbage Collection in Ruby]&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html Threads in Ruby]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67360</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67360"/>
		<updated>2012-10-10T09:22:45Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &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.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The most interesting fact about the use of modules is to define [http://en.wikipedia.org/wiki/Mixin mixins]. When a module is included within a class,all its functionality becomes available to the class.&lt;br /&gt;
*	Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How [http://en.wikipedia.org/wiki/Mixin mixins]is different from  #include and multiple inheritance? ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Let's see how [http://en.wikipedia.org/wiki/Mixin mixins]is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.&lt;br /&gt;
*	By using [http://en.wikipedia.org/wiki/Mixin mixins]the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Mixin mixins]corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;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;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++] (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a [http://en.wikipedia.org/wiki/Namespace namespace] is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Namespace Namespace] usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a [http://en.wikipedia.org/wiki/Namespace namespace]with the keyword [http://en.wikipedia.org/wiki/Namespace namespace].&lt;br /&gt;
*	To access the elements of an identified [http://en.wikipedia.org/wiki/Namespace namespace] by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne [http://en.wikipedia.org/wiki/Namespace namespace].  &lt;br /&gt;
*	In order to access this variables from the outside the [http://en.wikipedia.org/wiki/Namespace namespace], we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another [http://en.wikipedia.org/wiki/Namespace namespace]definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by [http://en.wikipedia.org/wiki/Destructor_(computer_programming) destructors].&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the [http://en.wikipedia.org/wiki/Interface_(Java) interface] agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an [http://en.wikipedia.org/wiki/Interface_(Java) interface], you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the [http://en.wikipedia.org/wiki/Interface_(Java) interface], the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	Aclass uses the implements keyword to implement an [http://en.wikipedia.org/wiki/Interface_(Java) interface]. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable] mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	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. &lt;br /&gt;
*	If the other object is not comparable then is should just return nil.[http://www.ruby-doc.org/core-1.9.3/Comparable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;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 [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable]mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.3/Enumerable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of [http://ruby-doc.org/core-1.9.3/Enumerable.html 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 [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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.&lt;br /&gt;
*	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;
*	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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
*	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;
*	For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	Interfaces are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 === Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Set based language - object specification	&lt;br /&gt;
! Protocol 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;
| 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;
| After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes. &lt;br /&gt;
| Multiple instances of that object are obtained by copying or cloning. &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.	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;
| 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;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* 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;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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]&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Ruby extend and Include]&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_modules.htm Ruby Multiple Inheritance with modules and mixins]&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Learn Ruby]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Perl Perl]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming Ruby: Program Like the Ruby Pros]&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&lt;br /&gt;
[http://www.rubyinside.com/how-ruby-manages-memory-and-garbage-collection-3006.html Garbage Collection in Ruby]&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html Threads in Ruby]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67359</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67359"/>
		<updated>2012-10-10T09:20:08Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
*	Here's an example taken from class notes Class notes of CSC517, NCSU to illustrate modules.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The most interesting fact about the use of modules is to define [http://en.wikipedia.org/wiki/Mixin mixins]. When a module is included within a class,all its functionality becomes available to the class.&lt;br /&gt;
*	Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How [http://en.wikipedia.org/wiki/Mixin mixins]is different from  #include and multiple inheritance? ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Let's see how [http://en.wikipedia.org/wiki/Mixin mixins]is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.&lt;br /&gt;
*	By using [http://en.wikipedia.org/wiki/Mixin mixins]the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Mixin mixins]corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example taken from class notes Class notes of CSC517, NCSU to illustrate mixins.&lt;br /&gt;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++] (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a [http://en.wikipedia.org/wiki/Namespace namespace] is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Namespace Namespace] usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a [http://en.wikipedia.org/wiki/Namespace namespace]with the keyword [http://en.wikipedia.org/wiki/Namespace namespace].&lt;br /&gt;
*	To access the elements of an identified [http://en.wikipedia.org/wiki/Namespace namespace] by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne [http://en.wikipedia.org/wiki/Namespace namespace].  &lt;br /&gt;
*	In order to access this variables from the outside the [http://en.wikipedia.org/wiki/Namespace namespace], we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another [http://en.wikipedia.org/wiki/Namespace namespace]definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by [http://en.wikipedia.org/wiki/Destructor_(computer_programming) destructors].&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the [http://en.wikipedia.org/wiki/Interface_(Java) interface] agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an [http://en.wikipedia.org/wiki/Interface_(Java) interface], you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the [http://en.wikipedia.org/wiki/Interface_(Java) interface], the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	Aclass uses the implements keyword to implement an [http://en.wikipedia.org/wiki/Interface_(Java) interface]. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable] mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	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. &lt;br /&gt;
*	If the other object is not comparable then is should just return nil.[http://www.ruby-doc.org/core-1.9.3/Comparable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Here is an example taken from class notes Class notes of CSC517, NCSU to illustrate [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable]mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.3/Enumerable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin in class,consider the following example taken from class notes Class notes of CSC517, NCSU. The class VowelFinder returns successive vowels in a string using the [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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.&lt;br /&gt;
*	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;
*	Given below is the Taggable-string example taken from the 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
*	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;
*	For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	Interfaces are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 === Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Set based language - object specification	&lt;br /&gt;
! Protocol 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;
| 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;
| After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes. &lt;br /&gt;
| Multiple instances of that object are obtained by copying or cloning. &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.	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;
| 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;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* The example below, which is taken from the Class notes of CSC517, NCSU illustrates the concept of extending specific objects.&lt;br /&gt;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  References ==&lt;br /&gt;
&lt;br /&gt;
Class notes of CSC517, NCSU&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Ruby extend and Include]&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_modules.htm Ruby Multiple Inheritance with modules and mixins]&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Learn Ruby]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Perl Perl]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming Ruby: Program Like the Ruby Pros]&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&lt;br /&gt;
[http://www.rubyinside.com/how-ruby-manages-memory-and-garbage-collection-3006.html Garbage Collection in Ruby]&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html Threads in Ruby]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67358</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67358"/>
		<updated>2012-10-10T09:19:15Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
*	Here's an example taken from class notes Class notes of CSC517, NCSU to illustrate modules.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The most interesting fact about the use of modules is to define [http://en.wikipedia.org/wiki/Mixin mixins]. When a module is included within a class,all its functionality becomes available to the class.&lt;br /&gt;
*	Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How [http://en.wikipedia.org/wiki/Mixin mixins]is different from  #include and multiple inheritance? ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Let's see how [http://en.wikipedia.org/wiki/Mixin mixins]is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.&lt;br /&gt;
*	By using [http://en.wikipedia.org/wiki/Mixin mixins]the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Mixin mixins]corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example taken from class notes Class notes of CSC517, NCSU to illustrate mixins.&lt;br /&gt;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++] (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a [http://en.wikipedia.org/wiki/Namespace namespace] is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Namespace Namespace] usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a [http://en.wikipedia.org/wiki/Namespace namespace]with the keyword [http://en.wikipedia.org/wiki/Namespace namespace].&lt;br /&gt;
*	To access the elements of an identified [http://en.wikipedia.org/wiki/Namespace namespace] by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne [http://en.wikipedia.org/wiki/Namespace namespace].  &lt;br /&gt;
*	In order to access this variables from the outside the [http://en.wikipedia.org/wiki/Namespace namespace], we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another [http://en.wikipedia.org/wiki/Namespace namespace]definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Multiple_inheritance inheritance] graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by [http://en.wikipedia.org/wiki/Destructor_(computer_programming) destructors].&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the [http://en.wikipedia.org/wiki/Interface_(Java) interface] agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an [http://en.wikipedia.org/wiki/Interface_(Java) interface], you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the [http://en.wikipedia.org/wiki/Interface_(Java) interface], the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	Aclass uses the implements keyword to implement an [http://en.wikipedia.org/wiki/Interface_(Java) interface]. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable] mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	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. &lt;br /&gt;
*	If the other object is not comparable then is should just return nil.[http://www.ruby-doc.org/core-1.9.3/Comparable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Here is an example taken from class notes Class notes of CSC517, NCSU to illustrate [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable]mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.3/Enumerable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin in class,consider the following example taken from class notes Class notes of CSC517, NCSU. The class VowelFinder returns successive vowels in a string using the [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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.&lt;br /&gt;
*	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;
*	Given below is the Taggable-string example taken from the 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
*	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;
*	For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	Interfaces are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 === Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Set based language - object specification	&lt;br /&gt;
! Protocol 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;
| 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;
| After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes. &lt;br /&gt;
| Multiple instances of that object are obtained by copying or cloning. &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.	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;
| 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;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* The example below, which is taken from the Class notes of CSC517, NCSU illustrates the concept of extending specific objects.&lt;br /&gt;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  References ==&lt;br /&gt;
&lt;br /&gt;
Class notes of CSC517, NCSU&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Ruby extend and Include]&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_modules.htm Ruby Multiple Inheritance with modules and mixins]&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Learn Ruby]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Perl Perl]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming Ruby: Program Like the Ruby Pros]&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&lt;br /&gt;
[http://www.rubyinside.com/how-ruby-manages-memory-and-garbage-collection-3006.html Garbage Collection in Ruby]&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html Threads in Ruby]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67357</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67357"/>
		<updated>2012-10-10T09:18:40Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
*	Here's an example taken from class notes Class notes of CSC517, NCSU to illustrate modules.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The most interesting fact about the use of modules is to define [http://en.wikipedia.org/wiki/Mixin mixins]. When a module is included within a class,all its functionality becomes available to the class.&lt;br /&gt;
*	Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How [http://en.wikipedia.org/wiki/Mixin mixins]is different from  #include and multiple inheritance? ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Let's see how [http://en.wikipedia.org/wiki/Mixin mixins]is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.&lt;br /&gt;
*	By using [http://en.wikipedia.org/wiki/Mixin mixins]the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Mixin mixins]corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example taken from class notes Class notes of CSC517, NCSU to illustrate mixins.&lt;br /&gt;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++] (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a [http://en.wikipedia.org/wiki/Namespace namespace] is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Namespace Namespace] usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a [http://en.wikipedia.org/wiki/Namespace namespace]with the keyword [http://en.wikipedia.org/wiki/Namespace namespace].&lt;br /&gt;
*	To access the elements of an identified [http://en.wikipedia.org/wiki/Namespace namespace] by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne [http://en.wikipedia.org/wiki/Namespace namespace].  &lt;br /&gt;
*	In order to access this variables from the outside the [http://en.wikipedia.org/wiki/Namespace namespace], we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another [http://en.wikipedia.org/wiki/Namespace namespace]definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Multiple_inheritance inheritance] graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by [http://en.wikipedia.org/wiki/Destructor_(computer_programming) destructors].&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the [http://en.wikipedia.org/wiki/Interface_(Java) interface] agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an [http://en.wikipedia.org/wiki/Interface_(Java) interface], you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the [http://en.wikipedia.org/wiki/Interface_(Java) interface], the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	Aclass uses the implements keyword to implement an [http://en.wikipedia.org/wiki/Interface_(Java) interface]. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable] mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	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. &lt;br /&gt;
*	If the other object is not comparable then is should just return nil.[http://www.ruby-doc.org/core-1.9.3/Comparable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Here is an example taken from class notes Class notes of CSC517, NCSU to illustrate [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable]mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.3/Enumerable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin in class,consider the following example taken from class notes Class notes of CSC517, NCSU. The class VowelFinder returns successive vowels in a string using the [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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.&lt;br /&gt;
*	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;
*	Given below is the Taggable-string example taken from the 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
*	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;
*	For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	Interfaces are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 === Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Set based language - object specification	&lt;br /&gt;
! Protocol 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;
| 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;
| After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes. &lt;br /&gt;
| Multiple instances of that object are obtained by copying or cloning. &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.	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;
| 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;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* The example below, which is taken from the Class notes of CSC517, NCSU illustrates the concept of extending specific objects.&lt;br /&gt;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  References ==&lt;br /&gt;
&lt;br /&gt;
Class notes of CSC517, NCSU&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Ruby extend and Include]&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_modules.htm Ruby Multiple Inheritance with modules and mixins]&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Learn Ruby]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Perl Perl]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming Ruby: Program Like the Ruby Pros]&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&lt;br /&gt;
[http://www.rubyinside.com/how-ruby-manages-memory-and-garbage-collection-3006.html Garbage Collection in Ruby]&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html Threads in Ruby]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67356</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67356"/>
		<updated>2012-10-10T09:18:06Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
*	Here's an example taken from class notes Class notes of CSC517, NCSU to illustrate modules.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The most interesting fact about the use of modules is to define [http://en.wikipedia.org/wiki/Mixin mixins]. When a module is included within a class,all its functionality becomes available to the class.&lt;br /&gt;
*	Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How [http://en.wikipedia.org/wiki/Mixin mixins]is different from  #include and multiple inheritance? ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Let's see how [http://en.wikipedia.org/wiki/Mixin mixins]is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.&lt;br /&gt;
*	By using [http://en.wikipedia.org/wiki/Mixin mixins]the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Mixin mixins]corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example taken from class notes Class notes of CSC517, NCSU to illustrate mixins.&lt;br /&gt;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++] (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a [http://en.wikipedia.org/wiki/Namespace namespace] is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Namespace Namespace] usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a [http://en.wikipedia.org/wiki/Namespace namespace]with the keyword [http://en.wikipedia.org/wiki/Namespace namespace].&lt;br /&gt;
*	To access the elements of an identified [http://en.wikipedia.org/wiki/Namespace namespace] by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne [http://en.wikipedia.org/wiki/Namespace namespace].  &lt;br /&gt;
*	In order to access this variables from the outside the [http://en.wikipedia.org/wiki/Namespace namespace], we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another [http://en.wikipedia.org/wiki/Namespace namespace]definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Multiple_inheritance inheritance] graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by [http://en.wikipedia.org/wiki/Destructor_(computer_programming) destructors].&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the [http://en.wikipedia.org/wiki/Interface_(Java) interface] agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an [http://en.wikipedia.org/wiki/Interface_(Java) interface], you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the [http://en.wikipedia.org/wiki/Interface_(Java) interface], the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	Aclass uses the implements keyword to implement an [http://en.wikipedia.org/wiki/Interface_(Java) interface]. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable] mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	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. &lt;br /&gt;
*	If the other object is not comparable then is should just return nil.[http://www.ruby-doc.org/core-1.9.3/Comparable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Here is an example taken from class notes Class notes of CSC517, NCSU to illustrate [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable]mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.3/Enumerable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin in class,consider the following example taken from class notes Class notes of CSC517, NCSU. The class VowelFinder returns successive vowels in a string using the [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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.&lt;br /&gt;
*	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;
*	Given below is the Taggable-string example taken from the 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
*	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;
*	For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	Interfaces are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 === Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Set based language - object specification	&lt;br /&gt;
! Protocol 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;
| 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;
| After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes. &lt;br /&gt;
| Multiple instances of that object are obtained by copying or cloning. &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.	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;
| 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;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* The example below, which is taken from the Class notes of CSC517, NCSU illustrates the concept of extending specific objects.&lt;br /&gt;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  References ==&lt;br /&gt;
&lt;br /&gt;
Class notes of CSC517, NCSU&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Ruby extend and Include]&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_modules.htm Ruby Multiple Inheritance with modules and mixins]&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Learn Ruby]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Perl Perl]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming Ruby: Program Like the Ruby Pros]&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&lt;br /&gt;
[http://www.rubyinside.com/how-ruby-manages-memory-and-garbage-collection-3006.html Garbage Collection in Ruby]&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html Threads in Ruby]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67355</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67355"/>
		<updated>2012-10-10T09:16:44Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/&amp;lt;pre&amp;gt;&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
*	Here's an example taken from class notes Class notes of CSC517, NCSU to illustrate modules.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The most interesting fact about the use of modules is to define [http://en.wikipedia.org/wiki/Mixin mixins]. When a module is included within a class,all its functionality becomes available to the class.&lt;br /&gt;
*	Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How [http://en.wikipedia.org/wiki/Mixin mixins]is different from  #include and multiple inheritance? ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Let's see how [http://en.wikipedia.org/wiki/Mixin mixins]is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.&lt;br /&gt;
*	By using [http://en.wikipedia.org/wiki/Mixin mixins]the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Mixin mixins]corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example taken from class notes Class notes of CSC517, NCSU to illustrate mixins.&lt;br /&gt;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++] (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a [http://en.wikipedia.org/wiki/Namespace namespace] is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Namespace Namespace] usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a [http://en.wikipedia.org/wiki/Namespace namespace]with the keyword [http://en.wikipedia.org/wiki/Namespace namespace].&lt;br /&gt;
*	To access the elements of an identified [http://en.wikipedia.org/wiki/Namespace namespace] by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne [http://en.wikipedia.org/wiki/Namespace namespace].  &lt;br /&gt;
*	In order to access this variables from the outside the [http://en.wikipedia.org/wiki/Namespace namespace], we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another [http://en.wikipedia.org/wiki/Namespace namespace]definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Multiple_inheritance inheritance] graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by [http://en.wikipedia.org/wiki/Destructor_(computer_programming) destructors].&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the [http://en.wikipedia.org/wiki/Interface_(Java) interface] agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an [http://en.wikipedia.org/wiki/Interface_(Java) interface], you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the [http://en.wikipedia.org/wiki/Interface_(Java) interface], the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	Aclass uses the implements keyword to implement an [http://en.wikipedia.org/wiki/Interface_(Java) interface]. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable] mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	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. &lt;br /&gt;
*	If the other object is not comparable then is should just return nil.[http://www.ruby-doc.org/core-1.9.3/Comparable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Here is an example taken from class notes Class notes of CSC517, NCSU to illustrate [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable]mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.3/Enumerable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin in class,consider the following example taken from class notes Class notes of CSC517, NCSU. The class VowelFinder returns successive vowels in a string using the [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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.&lt;br /&gt;
*	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;
*	Given below is the Taggable-string example taken from the 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
*	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;
*	For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	Interfaces are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 === Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Set based language - object specification	&lt;br /&gt;
! Protocol 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;
| 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;
| After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes. &lt;br /&gt;
| Multiple instances of that object are obtained by copying or cloning. &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.	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;
| 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;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* The example below, which is taken from the Class notes of CSC517, NCSU illustrates the concept of extending specific objects.&lt;br /&gt;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  References ==&lt;br /&gt;
&lt;br /&gt;
Class notes of CSC517, NCSU&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Ruby extend and Include]&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_modules.htm Ruby Multiple Inheritance with modules and mixins]&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Learn Ruby]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Perl Perl]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming Ruby: Program Like the Ruby Pros]&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&lt;br /&gt;
[http://www.rubyinside.com/how-ruby-manages-memory-and-garbage-collection-3006.html Garbage Collection in Ruby]&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html Threads in Ruby]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67354</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67354"/>
		<updated>2012-10-10T09:15:23Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/&amp;lt;pre&amp;gt;&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
*	Here's an example taken from class notes Class notes of CSC517, NCSU to illustrate modules.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The most interesting fact about the use of modules is to define [http://en.wikipedia.org/wiki/Mixin mixins]. When a module is included within a class,all its functionality becomes available to the class.&lt;br /&gt;
*	Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How [http://en.wikipedia.org/wiki/Mixin mixins]is different from  #include and multiple inheritance? ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Let's see how [http://en.wikipedia.org/wiki/Mixin mixins]is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.&lt;br /&gt;
*	By using [http://en.wikipedia.org/wiki/Mixin mixins]the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Mixin mixins]corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example taken from class notes Class notes of CSC517, NCSU to illustrate mixins.&lt;br /&gt;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++] (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a [http://en.wikipedia.org/wiki/Namespace namespace] is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Namespace Namespace] usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a [http://en.wikipedia.org/wiki/Namespace namespace]with the keyword [http://en.wikipedia.org/wiki/Namespace namespace].&lt;br /&gt;
*	To access the elements of an identified [http://en.wikipedia.org/wiki/Namespace namespace] by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne [http://en.wikipedia.org/wiki/Namespace namespace].  &lt;br /&gt;
*	In order to access this variables from the outside the [http://en.wikipedia.org/wiki/Namespace namespace], we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another [http://en.wikipedia.org/wiki/Namespace namespace]definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Multiple_inheritance inheritance] graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by [http://en.wikipedia.org/wiki/Destructor_(computer_programming) destructors].&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the [http://en.wikipedia.org/wiki/Interface_(Java) interface] agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an [http://en.wikipedia.org/wiki/Interface_(Java) interface], you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the [http://en.wikipedia.org/wiki/Interface_(Java) interface], the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	Aclass uses the implements keyword to implement an [http://en.wikipedia.org/wiki/Interface_(Java) interface]. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable] mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	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. &lt;br /&gt;
*	If the other object is not comparable then is should just return nil.[http://www.ruby-doc.org/core-1.9.3/Comparable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Here is an example taken from class notes Class notes of CSC517, NCSU to illustrate [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable]mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.3/Enumerable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin in class,consider the following example taken from class notes Class notes of CSC517, NCSU. The class VowelFinder returns successive vowels in a string using the [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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.&lt;br /&gt;
*	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;
*	Given below is the Taggable-string example taken from the 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
*	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;
*	For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	Interfaces are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 === Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| Set based language - object specification	&lt;br /&gt;
| Protocol 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;
| 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;
| After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes. &lt;br /&gt;
| Multiple instances of that object are obtained by copying or cloning. &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.	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;
| 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;
|&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* The example below, which is taken from the Class notes of CSC517, NCSU illustrates the concept of extending specific objects.&lt;br /&gt;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  References ==&lt;br /&gt;
&lt;br /&gt;
Class notes of CSC517, NCSU&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Ruby extend and Include]&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_modules.htm Ruby Multiple Inheritance with modules and mixins]&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Learn Ruby]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Perl Perl]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming Ruby: Program Like the Ruby Pros]&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&lt;br /&gt;
[http://www.rubyinside.com/how-ruby-manages-memory-and-garbage-collection-3006.html Garbage Collection in Ruby]&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html Threads in Ruby]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67353</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67353"/>
		<updated>2012-10-10T09:12:32Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/&amp;lt;pre&amp;gt;&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
*	Here's an example taken from class notes Class notes of CSC517, NCSU to illustrate modules.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The most interesting fact about the use of modules is to define [http://en.wikipedia.org/wiki/Mixin mixins]. When a module is included within a class,all its functionality becomes available to the class.&lt;br /&gt;
*	Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How [http://en.wikipedia.org/wiki/Mixin mixins]is different from  #include and multiple inheritance? ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Let's see how [http://en.wikipedia.org/wiki/Mixin mixins]is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.&lt;br /&gt;
*	By using [http://en.wikipedia.org/wiki/Mixin mixins]the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Mixin mixins]corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example taken from class notes Class notes of CSC517, NCSU to illustrate mixins.&lt;br /&gt;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++] (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a [http://en.wikipedia.org/wiki/Namespace namespace] is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Namespace Namespace] usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a [http://en.wikipedia.org/wiki/Namespace namespace]with the keyword [http://en.wikipedia.org/wiki/Namespace namespace].&lt;br /&gt;
*	To access the elements of an identified [http://en.wikipedia.org/wiki/Namespace namespace] by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne [http://en.wikipedia.org/wiki/Namespace namespace].  &lt;br /&gt;
*	In order to access this variables from the outside the [http://en.wikipedia.org/wiki/Namespace namespace], we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another [http://en.wikipedia.org/wiki/Namespace namespace]definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Multiple_inheritance inheritance] graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by [http://en.wikipedia.org/wiki/Destructor_(computer_programming) destructors].&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the [http://en.wikipedia.org/wiki/Interface_(Java) interface] agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an [http://en.wikipedia.org/wiki/Interface_(Java) interface], you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the [http://en.wikipedia.org/wiki/Interface_(Java) interface], the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	Aclass uses the implements keyword to implement an [http://en.wikipedia.org/wiki/Interface_(Java) interface]. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable] mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	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. &lt;br /&gt;
*	If the other object is not comparable then is should just return nil.[http://www.ruby-doc.org/core-1.9.3/Comparable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Here is an example taken from class notes Class notes of CSC517, NCSU to illustrate [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable]mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.3/Enumerable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin in class,consider the following example taken from class notes Class notes of CSC517, NCSU. The class VowelFinder returns successive vowels in a string using the [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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.&lt;br /&gt;
*	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;
*	Given below is the Taggable-string example taken from the 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
*	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;
*	For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	Interfaces are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 === Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
|Set based language - object specification	|Protocol 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.	| 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;
After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes. | Multiple instances of that object are obtained by copying or cloning. | |- |&lt;br /&gt;
Every object instance for a class is unique and holds its internal values for all the features defined for that class.	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;
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;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* The example below, which is taken from the Class notes of CSC517, NCSU illustrates the concept of extending specific objects.&lt;br /&gt;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  References ==&lt;br /&gt;
&lt;br /&gt;
Class notes of CSC517, NCSU&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Ruby extend and Include]&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_modules.htm Ruby Multiple Inheritance with modules and mixins]&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Learn Ruby]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Perl Perl]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming Ruby: Program Like the Ruby Pros]&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&lt;br /&gt;
[http://www.rubyinside.com/how-ruby-manages-memory-and-garbage-collection-3006.html Garbage Collection in Ruby]&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html Threads in Ruby]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67352</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67352"/>
		<updated>2012-10-10T09:02:41Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/&amp;lt;pre&amp;gt;&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
*	Here's an example taken from class notes Class notes of CSC517, NCSU to illustrate modules.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The most interesting fact about the use of modules is to define [http://en.wikipedia.org/wiki/Mixin mixins]. When a module is included within a class,all its functionality becomes available to the class.&lt;br /&gt;
*	Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How [http://en.wikipedia.org/wiki/Mixin mixins]is different from  #include and multiple inheritance? ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Let's see how [http://en.wikipedia.org/wiki/Mixin mixins]is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.&lt;br /&gt;
*	By using [http://en.wikipedia.org/wiki/Mixin mixins]the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Mixin mixins]corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example taken from class notes Class notes of CSC517, NCSU to illustrate mixins.&lt;br /&gt;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++] (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a [http://en.wikipedia.org/wiki/Namespace namespace] is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Namespace Namespace] usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a [http://en.wikipedia.org/wiki/Namespace namespace]with the keyword [http://en.wikipedia.org/wiki/Namespace namespace].&lt;br /&gt;
*	To access the elements of an identified [http://en.wikipedia.org/wiki/Namespace namespace] by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne [http://en.wikipedia.org/wiki/Namespace namespace].  &lt;br /&gt;
*	In order to access this variables from the outside the [http://en.wikipedia.org/wiki/Namespace namespace], we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another [http://en.wikipedia.org/wiki/Namespace namespace]definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Multiple_inheritance inheritance] graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by [http://en.wikipedia.org/wiki/Destructor_(computer_programming) destructors].&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the [http://en.wikipedia.org/wiki/Interface_(Java) interface] agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an [http://en.wikipedia.org/wiki/Interface_(Java) interface], you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the [http://en.wikipedia.org/wiki/Interface_(Java) interface], the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	Aclass uses the implements keyword to implement an [http://en.wikipedia.org/wiki/Interface_(Java) interface]. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable] mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	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. &lt;br /&gt;
*	If the other object is not comparable then is should just return nil.[http://www.ruby-doc.org/core-1.9.3/Comparable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Here is an example taken from class notes Class notes of CSC517, NCSU to illustrate [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable]mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.3/Enumerable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin in class,consider the following example taken from class notes Class notes of CSC517, NCSU. The class VowelFinder returns successive vowels in a string using the [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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.&lt;br /&gt;
*	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;
*	Given below is the Taggable-string example taken from the 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
*	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;
*	For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	Interfaces are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 === Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
Set based language - object specification	Protocol based language - object specification&lt;br /&gt;
First, a class is described which abstracts the features or properties of the object we want to specify.	 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;
After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes.	Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
Every object instance for a class is unique and holds its internal values for all the features defined for that class.	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;
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;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* The example below, which is taken from the Class notes of CSC517, NCSU illustrates the concept of extending specific objects.&lt;br /&gt;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  References ==&lt;br /&gt;
&lt;br /&gt;
Class notes of CSC517, NCSU&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Mixin Mixins]&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_composition Object Composition]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Is-a Is-a relatioship]&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Ruby extend and Include]&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_modules.htm Ruby Multiple Inheritance with modules and mixins]&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Learn Ruby]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Perl Perl]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming Ruby: Program Like the Ruby Pros]&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&lt;br /&gt;
[http://www.rubyinside.com/how-ruby-manages-memory-and-garbage-collection-3006.html Garbage Collection in Ruby]&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html Threads in Ruby]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67351</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67351"/>
		<updated>2012-10-10T09:00:45Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/&amp;lt;pre&amp;gt;&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
*	Here's an example taken from class notes Class notes of CSC517, NCSU to illustrate modules.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The most interesting fact about the use of modules is to define [http://en.wikipedia.org/wiki/Mixin mixins]. When a module is included within a class,all its functionality becomes available to the class.&lt;br /&gt;
*	Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How [http://en.wikipedia.org/wiki/Mixin mixins]is different from  #include and multiple inheritance? ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Let's see how [http://en.wikipedia.org/wiki/Mixin mixins]is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.&lt;br /&gt;
*	By using [http://en.wikipedia.org/wiki/Mixin mixins]the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Mixin mixins]corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example taken from class notes Class notes of CSC517, NCSU to illustrate mixins.&lt;br /&gt;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++] (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a [http://en.wikipedia.org/wiki/Namespace namespace] is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Namespace Namespace] usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a [http://en.wikipedia.org/wiki/Namespace namespace]with the keyword [http://en.wikipedia.org/wiki/Namespace namespace].&lt;br /&gt;
*	To access the elements of an identified [http://en.wikipedia.org/wiki/Namespace namespace] by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne [http://en.wikipedia.org/wiki/Namespace namespace].  &lt;br /&gt;
*	In order to access this variables from the outside the [http://en.wikipedia.org/wiki/Namespace namespace], we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another [http://en.wikipedia.org/wiki/Namespace namespace]definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Multiple_inheritance inheritance] graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by [http://en.wikipedia.org/wiki/Destructor_(computer_programming) destructors].&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the [http://en.wikipedia.org/wiki/Interface_(Java) interface] agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an [http://en.wikipedia.org/wiki/Interface_(Java) interface], you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the [http://en.wikipedia.org/wiki/Interface_(Java) interface], the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	Aclass uses the implements keyword to implement an [http://en.wikipedia.org/wiki/Interface_(Java) interface]. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable] mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	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. &lt;br /&gt;
*	If the other object is not comparable then is should just return nil.[http://www.ruby-doc.org/core-1.9.3/Comparable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Here is an example taken from class notes Class notes of CSC517, NCSU to illustrate [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable]mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.3/Enumerable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin in class,consider the following example taken from class notes Class notes of CSC517, NCSU. The class VowelFinder returns successive vowels in a string using the [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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.&lt;br /&gt;
*	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;
*	Given below is the Taggable-string example taken from the 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
*	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;
*	For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	Interfaces are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 === Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
Set based language - object specification	Protocol based language - object specification&lt;br /&gt;
First, a class is described which abstracts the features or properties of the object we want to specify.	 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;
After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes.	Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
Every object instance for a class is unique and holds its internal values for all the features defined for that class.	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;
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;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* The example below, which is taken from the Class notes of CSC517, NCSU illustrates the concept of extending specific objects.&lt;br /&gt;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  References ==&lt;br /&gt;
&lt;br /&gt;
Class notes of CSC517, NCSU&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
Deadly Diamond of Death&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses&lt;br /&gt;
Mixins&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
Object Composition&lt;br /&gt;
Is-a relatioship&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Compile_time Compile time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Run_time Run time]&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Ruby extend and Include]&lt;br /&gt;
[http://www.tutorialspoint.com/ruby/ruby_modules.htm Ruby Multiple Inheritance with modules and mixins]&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Learn Ruby]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Perl Perl]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming Ruby: Program Like the Ruby Pros]&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&lt;br /&gt;
[http://www.rubyinside.com/how-ruby-manages-memory-and-garbage-collection-3006.html Garbage Collection in Ruby]&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html Threads in Ruby]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67350</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67350"/>
		<updated>2012-10-10T08:57:45Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression 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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/&amp;lt;pre&amp;gt;&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
*	Here's an example taken from class notes Class notes of CSC517, NCSU to illustrate modules.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The most interesting fact about the use of modules is to define [http://en.wikipedia.org/wiki/Mixin mixins]. When a module is included within a class,all its functionality becomes available to the class.&lt;br /&gt;
*	Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How [http://en.wikipedia.org/wiki/Mixin mixins]is different from  #include and multiple inheritance? ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Let's see how [http://en.wikipedia.org/wiki/Mixin mixins]is contrasting to #include and multiple inheritance in other languages.#include files define methods that can be called but not applied to objects.&lt;br /&gt;
*	By using [http://en.wikipedia.org/wiki/Mixin mixins]the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Mixin mixins]corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example taken from class notes Class notes of CSC517, NCSU to illustrate mixins.&lt;br /&gt;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in [http://en.wikipedia.org/wiki/C%2B%2B C++] (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a [http://en.wikipedia.org/wiki/Namespace namespace] is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Namespace Namespace] usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a [http://en.wikipedia.org/wiki/Namespace namespace]with the keyword [http://en.wikipedia.org/wiki/Namespace namespace].&lt;br /&gt;
*	To access the elements of an identified [http://en.wikipedia.org/wiki/Namespace namespace] by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne [http://en.wikipedia.org/wiki/Namespace namespace].  &lt;br /&gt;
*	In order to access this variables from the outside the [http://en.wikipedia.org/wiki/Namespace namespace], we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another [http://en.wikipedia.org/wiki/Namespace namespace]definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Multiple_inheritance inheritance] graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by [http://en.wikipedia.org/wiki/Destructor_(computer_programming) destructors].&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the [http://en.wikipedia.org/wiki/Interface_(Java) interface] agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an [http://en.wikipedia.org/wiki/Interface_(Java) interface], you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the [http://en.wikipedia.org/wiki/Interface_(Java) interface], the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	Aclass uses the implements keyword to implement an [http://en.wikipedia.org/wiki/Interface_(Java) interface]. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable] mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	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. &lt;br /&gt;
*	If the other object is not comparable then is should just return nil.[http://www.ruby-doc.org/core-1.9.3/Comparable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Here is an example taken from class notes Class notes of CSC517, NCSU to illustrate [http://www.ruby-doc.org/core-1.9.3/Comparable.html comparable]mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://ruby-doc.org/core-1.9.3/Enumerable.html 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin in class,consider the following example taken from class notes Class notes of CSC517, NCSU. The class VowelFinder returns successive vowels in a string using the [http://ruby-doc.org/core-1.9.3/Enumerable.html Enumerable]mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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.&lt;br /&gt;
*	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;
*	Given below is the Taggable-string example taken from the 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
*	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;
*	For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	Interfaces are defined by [http://en.wikipedia.org/wiki/Abstract_type Abstract Classes]. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 === Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
Set based language - object specification	Protocol based language - object specification&lt;br /&gt;
First, a class is described which abstracts the features or properties of the object we want to specify.	 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;
After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes.	Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
Every object instance for a class is unique and holds its internal values for all the features defined for that class.	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;
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;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* The example below, which is taken from the Class notes of CSC517, NCSU illustrates the concept of extending specific objects.&lt;br /&gt;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  References ==&lt;br /&gt;
&lt;br /&gt;
Class notes of CSC517, NCSU&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
Deadly Diamond of Death&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses&lt;br /&gt;
Mixins&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
Object Composition&lt;br /&gt;
Is-a relatioship&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
Compile time&lt;br /&gt;
Run time&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
Ruby extend and Include&lt;br /&gt;
Ruby Multiple Inheritance with modules and mixins&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstract_type Abstract types]&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Learn Ruby]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Functional_programming Functional programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Imperative_programming Imperative Programming]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Interpreted_language Interpreted language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_programming_language Dynamic language]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Python_(programming_language) Python]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Perl Perl]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model View Controller - MVC]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming Ruby: Program Like the Ruby Pros]&lt;br /&gt;
[http://designpatternsinruby.com/ Design Patterns in Ruby]&lt;br /&gt;
[http://www.rubyinside.com/how-ruby-manages-memory-and-garbage-collection-3006.html Garbage Collection in Ruby]&lt;br /&gt;
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html Threads in Ruby]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67349</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67349"/>
		<updated>2012-10-10T08:31:30Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression Regular expressions] are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. &lt;br /&gt;
*	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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/&amp;lt;pre&amp;gt;&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
*	Here's an example taken from class notes Class notes of CSC517, NCSU to illustrate modules.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&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.&lt;br /&gt;
*	Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How mixins is different from  #include and multiple inheritance? ====&lt;br /&gt;
&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.&lt;br /&gt;
*	By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	Mixins corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example taken from class notes Class notes of CSC517, NCSU to illustrate mixins.&lt;br /&gt;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in C++ (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a namespace is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	Namespaces usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a namespace with the keyword namespace.&lt;br /&gt;
*	To access the elements of an identified namespace by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne namespace.  &lt;br /&gt;
*	In order to access this variables from the outside the namespace, we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another namespace definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by destructors.&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the interface agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the interface, the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	Aclass uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The comparable mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Here is an example taken from class notes Class notes of CSC517, NCSU to illustrate comparable mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of Enumerable mixin in class,consider the following example taken from class notes Class notes of CSC517, NCSU. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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.&lt;br /&gt;
*	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;
*	Given below is the Taggable-string example taken from the 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
*	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;
*	For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 === Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
Set based language - object specification	Protocol based language - object specification&lt;br /&gt;
First, a class is described which abstracts the features or properties of the object we want to specify.	 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;
After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes.	Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
Every object instance for a class is unique and holds its internal values for all the features defined for that class.	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;
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;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* The example below, which is taken from the Class notes of CSC517, NCSU illustrates the concept of extending specific objects.&lt;br /&gt;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  References ==&lt;br /&gt;
&lt;br /&gt;
Class notes of CSC517, NCSU&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
Deadly Diamond of Death&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses&lt;br /&gt;
Mixins&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
Object Composition&lt;br /&gt;
Is-a relatioship&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
Compile time&lt;br /&gt;
Run time&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
Ruby extend and Include&lt;br /&gt;
Ruby Multiple Inheritance with modules and mixins&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
Abstract types&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
Object oriented Programming&lt;br /&gt;
Learn Ruby&lt;br /&gt;
Functional programming&lt;br /&gt;
Imperative Programming&lt;br /&gt;
Interpreted language&lt;br /&gt;
Reflection&lt;br /&gt;
Dynamic language&lt;br /&gt;
Python&lt;br /&gt;
Perl&lt;br /&gt;
Ruby on Rails&lt;br /&gt;
Model View Controller - MVC&lt;br /&gt;
Metaprogramming Ruby: Program Like the Ruby Pros&lt;br /&gt;
Design Patterns in Ruby&lt;br /&gt;
Garbage Collection in Ruby&lt;br /&gt;
Threads in Ruby&lt;br /&gt;
Ruby interface to C modules&lt;br /&gt;
Currying&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67348</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67348"/>
		<updated>2012-10-10T08:30:45Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	[http://en.wikipedia.org/wiki/Regular_expression/ Regular expressions] are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. &lt;br /&gt;
*	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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/&amp;lt;pre&amp;gt;&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
*	Here's an example taken from class notes Class notes of CSC517, NCSU to illustrate modules.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&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.&lt;br /&gt;
*	Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How mixins is different from  #include and multiple inheritance? ====&lt;br /&gt;
&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.&lt;br /&gt;
*	By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	Mixins corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example taken from class notes Class notes of CSC517, NCSU to illustrate mixins.&lt;br /&gt;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in C++ (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a namespace is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	Namespaces usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a namespace with the keyword namespace.&lt;br /&gt;
*	To access the elements of an identified namespace by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne namespace.  &lt;br /&gt;
*	In order to access this variables from the outside the namespace, we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another namespace definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by destructors.&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the interface agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the interface, the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	Aclass uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The comparable mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Here is an example taken from class notes Class notes of CSC517, NCSU to illustrate comparable mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of Enumerable mixin in class,consider the following example taken from class notes Class notes of CSC517, NCSU. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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.&lt;br /&gt;
*	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;
*	Given below is the Taggable-string example taken from the 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
*	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;
*	For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 === Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
Set based language - object specification	Protocol based language - object specification&lt;br /&gt;
First, a class is described which abstracts the features or properties of the object we want to specify.	 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;
After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes.	Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
Every object instance for a class is unique and holds its internal values for all the features defined for that class.	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;
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;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* The example below, which is taken from the Class notes of CSC517, NCSU illustrates the concept of extending specific objects.&lt;br /&gt;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  References ==&lt;br /&gt;
&lt;br /&gt;
Class notes of CSC517, NCSU&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
Deadly Diamond of Death&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses&lt;br /&gt;
Mixins&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
Object Composition&lt;br /&gt;
Is-a relatioship&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
Compile time&lt;br /&gt;
Run time&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
Ruby extend and Include&lt;br /&gt;
Ruby Multiple Inheritance with modules and mixins&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
Abstract types&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
Object oriented Programming&lt;br /&gt;
Learn Ruby&lt;br /&gt;
Functional programming&lt;br /&gt;
Imperative Programming&lt;br /&gt;
Interpreted language&lt;br /&gt;
Reflection&lt;br /&gt;
Dynamic language&lt;br /&gt;
Python&lt;br /&gt;
Perl&lt;br /&gt;
Ruby on Rails&lt;br /&gt;
Model View Controller - MVC&lt;br /&gt;
Metaprogramming Ruby: Program Like the Ruby Pros&lt;br /&gt;
Design Patterns in Ruby&lt;br /&gt;
Garbage Collection in Ruby&lt;br /&gt;
Threads in Ruby&lt;br /&gt;
Ruby interface to C modules&lt;br /&gt;
Currying&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67289</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=67289"/>
		<updated>2012-10-05T05:05:02Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. &lt;br /&gt;
*	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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/&amp;lt;pre&amp;gt;&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
*	Here's an example taken from class notes Class notes of CSC517, NCSU to illustrate modules.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&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.&lt;br /&gt;
*	Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How mixins is different from  #include and multiple inheritance? ====&lt;br /&gt;
&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.&lt;br /&gt;
*	By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	Mixins corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example taken from class notes Class notes of CSC517, NCSU to illustrate mixins.&lt;br /&gt;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in C++ (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a namespace is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	Namespaces usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a namespace with the keyword namespace.&lt;br /&gt;
*	To access the elements of an identified namespace by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne namespace.  &lt;br /&gt;
*	In order to access this variables from the outside the namespace, we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another namespace definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by destructors.&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the interface agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the interface, the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	Aclass uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The comparable mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Here is an example taken from class notes Class notes of CSC517, NCSU to illustrate comparable mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of Enumerable mixin in class,consider the following example taken from class notes Class notes of CSC517, NCSU. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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.&lt;br /&gt;
*	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;
*	Given below is the Taggable-string example taken from the 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
*	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;
*	For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 === Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
Set based language - object specification	Protocol based language - object specification&lt;br /&gt;
First, a class is described which abstracts the features or properties of the object we want to specify.	 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;
After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes.	Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
Every object instance for a class is unique and holds its internal values for all the features defined for that class.	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;
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;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* The example below, which is taken from the Class notes of CSC517, NCSU illustrates the concept of extending specific objects.&lt;br /&gt;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  References ==&lt;br /&gt;
&lt;br /&gt;
Class notes of CSC517, NCSU&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
Deadly Diamond of Death&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses&lt;br /&gt;
Mixins&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
Object Composition&lt;br /&gt;
Is-a relatioship&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
Compile time&lt;br /&gt;
Run time&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
Ruby extend and Include&lt;br /&gt;
Ruby Multiple Inheritance with modules and mixins&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
Abstract types&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
Object oriented Programming&lt;br /&gt;
Learn Ruby&lt;br /&gt;
Functional programming&lt;br /&gt;
Imperative Programming&lt;br /&gt;
Interpreted language&lt;br /&gt;
Reflection&lt;br /&gt;
Dynamic language&lt;br /&gt;
Python&lt;br /&gt;
Perl&lt;br /&gt;
Ruby on Rails&lt;br /&gt;
Model View Controller - MVC&lt;br /&gt;
Metaprogramming Ruby: Program Like the Ruby Pros&lt;br /&gt;
Design Patterns in Ruby&lt;br /&gt;
Garbage Collection in Ruby&lt;br /&gt;
Threads in Ruby&lt;br /&gt;
Ruby interface to C modules&lt;br /&gt;
Currying&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=66907</id>
		<title>CSC/ECE 517 Fall 2012/ch1b 1w37 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1b_1w37_ss&amp;diff=66907"/>
		<updated>2012-10-04T00:45:08Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: Created page with &amp;quot;Modules and Mixins     ==  Regular Expressions ==   ===  Definition ===     *	Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. &lt;br /&gt;
*	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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;nowiki&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
*	Here's an example taken from class notes Class notes of CSC517, NCSU to illustrate modules.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&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.&lt;br /&gt;
*	Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How mixins is different from  #include and multiple inheritance? ====&lt;br /&gt;
&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.&lt;br /&gt;
*	By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	Mixins corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example taken from class notes Class notes of CSC517, NCSU to illustrate mixins.&lt;br /&gt;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in C++ (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a namespace is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	Namespaces usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a namespace with the keyword namespace.&lt;br /&gt;
*	To access the elements of an identified namespace by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne namespace.  &lt;br /&gt;
*	In order to access this variables from the outside the namespace, we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another namespace definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by destructors.&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the interface agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the interface, the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	Aclass uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The comparable mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Here is an example taken from class notes Class notes of CSC517, NCSU to illustrate comparable mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of Enumerable mixin in class,consider the following example taken from class notes Class notes of CSC517, NCSU. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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.&lt;br /&gt;
*	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;
*	Given below is the Taggable-string example taken from the 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
*	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;
*	For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 === Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
Set based language - object specification	Protocol based language - object specification&lt;br /&gt;
First, a class is described which abstracts the features or properties of the object we want to specify.	 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;
After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes.	Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
Every object instance for a class is unique and holds its internal values for all the features defined for that class.	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;
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;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* The example below, which is taken from the Class notes of CSC517, NCSU illustrates the concept of extending specific objects.&lt;br /&gt;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  References ==&lt;br /&gt;
&lt;br /&gt;
Class notes of CSC517, NCSU&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
Deadly Diamond of Death&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses&lt;br /&gt;
Mixins&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
Object Composition&lt;br /&gt;
Is-a relatioship&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
Compile time&lt;br /&gt;
Run time&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
Ruby extend and Include&lt;br /&gt;
Ruby Multiple Inheritance with modules and mixins&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
Abstract types&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
Object oriented Programming&lt;br /&gt;
Learn Ruby&lt;br /&gt;
Functional programming&lt;br /&gt;
Imperative Programming&lt;br /&gt;
Interpreted language&lt;br /&gt;
Reflection&lt;br /&gt;
Dynamic language&lt;br /&gt;
Python&lt;br /&gt;
Perl&lt;br /&gt;
Ruby on Rails&lt;br /&gt;
Model View Controller - MVC&lt;br /&gt;
Metaprogramming Ruby: Program Like the Ruby Pros&lt;br /&gt;
Design Patterns in Ruby&lt;br /&gt;
Garbage Collection in Ruby&lt;br /&gt;
Threads in Ruby&lt;br /&gt;
Ruby interface to C modules&lt;br /&gt;
Currying&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=66906</id>
		<title>CSC/ECE 517 Fall 2012</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=66906"/>
		<updated>2012-10-04T00:44:52Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE 517 Fall 2012/ch1 n xx]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w1 rk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w20 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w5 su]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w6 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w4 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w7 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w8 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w9 av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w10 pk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w11 ap]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w12 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w14 gv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w17 ir]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w22 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 wi]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w31 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w16 br]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w23 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w24 nr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w15 rt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w3 pl]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w32 cm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w37 ss]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w67 ks]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w27 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w29 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w33 op]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w19 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w34 vd]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w35 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w30 rp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w47 sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w69 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w44 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w45 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w53 kc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 ar]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w39 sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w54 go]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w56 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w64 nn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w66 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w42 js]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w46 sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w71 gs]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w63 dv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w55 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w57 mp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w52 an]]&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=User:Psivash2&amp;diff=66327</id>
		<title>User:Psivash2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=User:Psivash2&amp;diff=66327"/>
		<updated>2012-10-03T04:43:13Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. &lt;br /&gt;
*	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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;nowiki&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
*	Here's an example taken from class notes Class notes of CSC517, NCSU to illustrate modules.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&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.&lt;br /&gt;
*	Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How mixins is different from  #include and multiple inheritance? ====&lt;br /&gt;
&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.&lt;br /&gt;
*	By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	Mixins corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example taken from class notes Class notes of CSC517, NCSU to illustrate mixins.&lt;br /&gt;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in C++ (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a namespace is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	Namespaces usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a namespace with the keyword namespace.&lt;br /&gt;
*	To access the elements of an identified namespace by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne namespace.  &lt;br /&gt;
*	In order to access this variables from the outside the namespace, we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another namespace definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by destructors.&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the interface agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*	When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the interface, the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	Aclass uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The comparable mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Here is an example taken from class notes Class notes of CSC517, NCSU to illustrate comparable mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example.&lt;br /&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;
*	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;
*	To understand the working of Enumerable mixin in class,consider the following example taken from class notes Class notes of CSC517, NCSU. The class VowelFinder returns successive vowels in a string using the Enumerable mixin method.&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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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.&lt;br /&gt;
*	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;
*	Given below is the Taggable-string example taken from the 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &amp;lt;pre&amp;gt;&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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
*	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;
*	For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 === Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;Deadly Diamond of Death&amp;quot;. This is illustrated by the Java code example below:&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
Set based language - object specification	Protocol based language - object specification&lt;br /&gt;
First, a class is described which abstracts the features or properties of the object we want to specify.	 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;
After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes.	Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
Every object instance for a class is unique and holds its internal values for all the features defined for that class.	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;
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;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* The example below, which is taken from the Class notes of CSC517, NCSU illustrates the concept of extending specific objects.&lt;br /&gt;
* Consider a Person class which has properties for a mild-mannered people.&lt;br /&gt;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&amp;lt;pre&amp;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;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==  References ==&lt;br /&gt;
&lt;br /&gt;
Class notes of CSC517, NCSU&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
Deadly Diamond of Death&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses&lt;br /&gt;
Mixins&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
Object Composition&lt;br /&gt;
Is-a relatioship&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
Compile time&lt;br /&gt;
Run time&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
Ruby extend and Include&lt;br /&gt;
Ruby Multiple Inheritance with modules and mixins&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
Abstract types&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
Object oriented Programming&lt;br /&gt;
Learn Ruby&lt;br /&gt;
Functional programming&lt;br /&gt;
Imperative Programming&lt;br /&gt;
Interpreted language&lt;br /&gt;
Reflection&lt;br /&gt;
Dynamic language&lt;br /&gt;
Python&lt;br /&gt;
Perl&lt;br /&gt;
Ruby on Rails&lt;br /&gt;
Model View Controller - MVC&lt;br /&gt;
Metaprogramming Ruby: Program Like the Ruby Pros&lt;br /&gt;
Design Patterns in Ruby&lt;br /&gt;
Garbage Collection in Ruby&lt;br /&gt;
Threads in Ruby&lt;br /&gt;
Ruby interface to C modules&lt;br /&gt;
Currying&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=User:Psivash2&amp;diff=66325</id>
		<title>User:Psivash2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=User:Psivash2&amp;diff=66325"/>
		<updated>2012-10-03T04:39:34Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. &lt;br /&gt;
*	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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. 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;
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;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;nowiki&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example	Description&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&amp;lt;pre/&amp;gt;&lt;br /&gt;
Example&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
*	Here's an example taken from class notes Class notes of CSC517, NCSU to illustrate modules.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&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.&lt;br /&gt;
*	Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How mixins is different from  #include and multiple inheritance? ====&lt;br /&gt;
&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.&lt;br /&gt;
*	By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	Mixins corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Consider the following example taken from class notes Class notes of CSC517, NCSU to illustrate mixins.&lt;br /&gt;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in C++ (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a namespace is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	Namespaces usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a namespace with the keyword namespace.&lt;br /&gt;
*	To access the elements of an identified namespace by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne namespace.  &lt;br /&gt;
*	In order to access this variables from the outside the namespace, we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another namespace definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by destructors.&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the interface agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&lt;br /&gt;
*	When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the interface, the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	Aclass uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The comparable mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an example taken from class notes Class notes of CSC517, NCSU to illustrate comparable mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&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;
&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;
*	To understand the working of Enumerable mixin in class,consider the following example taken from class notes 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;
&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;
&lt;br /&gt;
&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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.&lt;br /&gt;
*	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;
*	Given below is the Taggable-string example taken from the 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &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;
&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
*	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;
*	For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 === Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
Set based language - object specification	Protocol based language - object specification&lt;br /&gt;
First, a class is described which abstracts the features or properties of the object we want to specify.	 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;
After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes.	Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
Every object instance for a class is unique and holds its internal values for all the features defined for that class.	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;
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;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* The example below, which is taken from the Class notes of CSC517, NCSU illustrates the concept of extending specific objects.&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;
&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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&lt;br /&gt;
==  References ==&lt;br /&gt;
&lt;br /&gt;
Class notes of CSC517, NCSU&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
Deadly Diamond of Death&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses&lt;br /&gt;
Mixins&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
Object Composition&lt;br /&gt;
Is-a relatioship&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
Compile time&lt;br /&gt;
Run time&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
Ruby extend and Include&lt;br /&gt;
Ruby Multiple Inheritance with modules and mixins&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
Abstract types&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
Object oriented Programming&lt;br /&gt;
Learn Ruby&lt;br /&gt;
Functional programming&lt;br /&gt;
Imperative Programming&lt;br /&gt;
Interpreted language&lt;br /&gt;
Reflection&lt;br /&gt;
Dynamic language&lt;br /&gt;
Python&lt;br /&gt;
Perl&lt;br /&gt;
Ruby on Rails&lt;br /&gt;
Model View Controller - MVC&lt;br /&gt;
Metaprogramming Ruby: Program Like the Ruby Pros&lt;br /&gt;
Design Patterns in Ruby&lt;br /&gt;
Garbage Collection in Ruby&lt;br /&gt;
Threads in Ruby&lt;br /&gt;
Ruby interface to C modules&lt;br /&gt;
Currying&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=User:Psivash2&amp;diff=66321</id>
		<title>User:Psivash2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=User:Psivash2&amp;diff=66321"/>
		<updated>2012-10-03T04:35:37Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. &lt;br /&gt;
*	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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. Here's an example to retrieve the first match of /w.ll/ in the string.&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;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&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;
	&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;
&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;
&lt;br /&gt;
&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The table below represents some special characters and their meaning in the patterns.&lt;br /&gt;
&amp;lt;nowiki&amp;gt;&lt;br /&gt;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
*	Here's an example taken from class notes Class notes of CSC517, NCSU to illustrate modules.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mixins ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&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.&lt;br /&gt;
*	Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How mixins is different from  #include and multiple inheritance? ====&lt;br /&gt;
&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.&lt;br /&gt;
*	By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	Mixins corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from class notes Class notes of CSC517, NCSU to illustrate mixins.&lt;br /&gt;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in C++ (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a namespace is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	Namespaces usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a namespace with the keyword namespace.&lt;br /&gt;
*	To access the elements of an identified namespace by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne namespace.  &lt;br /&gt;
*	In order to access this variables from the outside the namespace, we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another namespace definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by destructors.&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the interface agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&lt;br /&gt;
*	When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the interface, the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	Aclass uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The comparable mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an example taken from class notes Class notes of CSC517, NCSU to illustrate comparable mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&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;
&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;
*	To understand the working of Enumerable mixin in class,consider the following example taken from class notes 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;
&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;
&lt;br /&gt;
&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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.&lt;br /&gt;
*	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;
*	Given below is the Taggable-string example taken from the 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &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;
&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
*	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;
*	For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 === Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
Set based language - object specification	Protocol based language - object specification&lt;br /&gt;
First, a class is described which abstracts the features or properties of the object we want to specify.	 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;
After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes.	Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
Every object instance for a class is unique and holds its internal values for all the features defined for that class.	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;
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;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* The example below, which is taken from the Class notes of CSC517, NCSU illustrates the concept of extending specific objects.&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;
&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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&lt;br /&gt;
==  References ==&lt;br /&gt;
&lt;br /&gt;
Class notes of CSC517, NCSU&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
Deadly Diamond of Death&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses&lt;br /&gt;
Mixins&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
Object Composition&lt;br /&gt;
Is-a relatioship&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
Compile time&lt;br /&gt;
Run time&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
Ruby extend and Include&lt;br /&gt;
Ruby Multiple Inheritance with modules and mixins&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
Abstract types&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
Object oriented Programming&lt;br /&gt;
Learn Ruby&lt;br /&gt;
Functional programming&lt;br /&gt;
Imperative Programming&lt;br /&gt;
Interpreted language&lt;br /&gt;
Reflection&lt;br /&gt;
Dynamic language&lt;br /&gt;
Python&lt;br /&gt;
Perl&lt;br /&gt;
Ruby on Rails&lt;br /&gt;
Model View Controller - MVC&lt;br /&gt;
Metaprogramming Ruby: Program Like the Ruby Pros&lt;br /&gt;
Design Patterns in Ruby&lt;br /&gt;
Garbage Collection in Ruby&lt;br /&gt;
Threads in Ruby&lt;br /&gt;
Ruby interface to C modules&lt;br /&gt;
Currying&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=User:Psivash2&amp;diff=66320</id>
		<title>User:Psivash2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=User:Psivash2&amp;diff=66320"/>
		<updated>2012-10-03T04:35:00Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
*	Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. &lt;br /&gt;
*	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. &lt;br /&gt;
*	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. Here's an example to retrieve the first match of /w.ll/ in the string.&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;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&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;
	&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;
&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;
&lt;br /&gt;
&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&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;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
*	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
*	Here's an example taken from class notes Class notes of CSC517, NCSU to illustrate modules.&lt;br /&gt;
*	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;
*	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.&lt;br /&gt;
*	The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mixins ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&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.&lt;br /&gt;
*	Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How mixins is different from  #include and multiple inheritance? ====&lt;br /&gt;
&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.&lt;br /&gt;
*	By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
*	Mixins corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from class notes Class notes of CSC517, NCSU to illustrate mixins.&lt;br /&gt;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in C++ (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	In general, a namespace is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
*	Namespaces usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
*	To uniquely identify a namespace with the keyword namespace.&lt;br /&gt;
*	To access the elements of an identified namespace by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
*	p and q are normal variables but integrated within the NewOne namespace.  &lt;br /&gt;
*	In order to access this variables from the outside the namespace, we have to use the scope operator ::. &lt;br /&gt;
*	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another namespace definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by destructors.&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
*	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
*	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
*	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
*	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
*	A class that implements the interface agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
*	An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
*	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
*	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&lt;br /&gt;
*	When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
*	If a class does not perform all the behaviors of the interface, the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
*	Aclass uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	The comparable mixin is used by classes whose objects may be ordered.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an example taken from class notes Class notes of CSC517, NCSU to illustrate comparable mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&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;
&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;
*	To understand the working of Enumerable mixin in class,consider the following example taken from class notes 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;
&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;
&lt;br /&gt;
&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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.&lt;br /&gt;
*	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;
*	Given below is the Taggable-string example taken from the 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &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;
&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
*	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;
*	For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
*	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
*	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
*	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
*	Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. &lt;br /&gt;
*	The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing the interfaces. &lt;br /&gt;
*	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
*	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
*	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 === Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
*	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. &lt;br /&gt;
*	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;
*	The super-classes may be correct and consistent. &lt;br /&gt;
*	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;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. &lt;br /&gt;
*	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;
Example &lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&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. &lt;br /&gt;
*	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
*	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
*	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
*	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
*	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
*	ApplePie has-a Apple&lt;br /&gt;
*	ApplePie has-a Cinnamon&lt;br /&gt;
*	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
Set based language - object specification	Protocol based language - object specification&lt;br /&gt;
First, a class is described which abstracts the features or properties of the object we want to specify.	 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;
After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes.	Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
Every object instance for a class is unique and holds its internal values for all the features defined for that class.	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;
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;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
* Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ruby can act as a set-based language.&lt;br /&gt;
* We can first define classes that form the blueprint or a template.&lt;br /&gt;
* We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
* We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
* The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
* 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;
* The example below, which is taken from the Class notes of CSC517, NCSU illustrates the concept of extending specific objects.&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;
&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;
Extending Objects - example&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. &lt;br /&gt;
*	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;
&lt;br /&gt;
==  References ==&lt;br /&gt;
&lt;br /&gt;
Class notes of CSC517, NCSU&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
Deadly Diamond of Death&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses&lt;br /&gt;
Mixins&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
Object Composition&lt;br /&gt;
Is-a relatioship&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
Compile time&lt;br /&gt;
Run time&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
Ruby extend and Include&lt;br /&gt;
Ruby Multiple Inheritance with modules and mixins&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
Abstract types&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
Object oriented Programming&lt;br /&gt;
Learn Ruby&lt;br /&gt;
Functional programming&lt;br /&gt;
Imperative Programming&lt;br /&gt;
Interpreted language&lt;br /&gt;
Reflection&lt;br /&gt;
Dynamic language&lt;br /&gt;
Python&lt;br /&gt;
Perl&lt;br /&gt;
Ruby on Rails&lt;br /&gt;
Model View Controller - MVC&lt;br /&gt;
Metaprogramming Ruby: Program Like the Ruby Pros&lt;br /&gt;
Design Patterns in Ruby&lt;br /&gt;
Garbage Collection in Ruby&lt;br /&gt;
Threads in Ruby&lt;br /&gt;
Ruby interface to C modules&lt;br /&gt;
Currying&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=User:Psivash2&amp;diff=66315</id>
		<title>User:Psivash2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=User:Psivash2&amp;diff=66315"/>
		<updated>2012-10-03T04:29:47Z</updated>

		<summary type="html">&lt;p&gt;Psivash2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Modules and Mixins  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Regular Expressions ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===  Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
•	Regular expressions are extremely powerful.Ruby was built as a better Perl hence it supports regular expressions. &lt;br /&gt;
•	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. &lt;br /&gt;
•	To understand the power of regular expressions here is an example.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Regexp objects ===&lt;br /&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;
r = /my regular expression/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Replace ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	In the following example the character 'c' is replaced with &amp;quot;&amp;quot;.&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;
&lt;br /&gt;
&lt;br /&gt;
=== Search ===&lt;br /&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. Here's an example to retrieve the first match of /w.ll/ in the string.&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;
&lt;br /&gt;
Suppose you want to retrieve the first digit in a string.Here's an example:&lt;br /&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;
	&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;
&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;
&lt;br /&gt;
&lt;br /&gt;
=== List of commonly used Regular expressions ===&lt;br /&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;
Regex- Character	Meaning&lt;br /&gt;
(...)	Capture everything enclosed&lt;br /&gt;
(a|b)	a or b&lt;br /&gt;
a?	Zero or one of a&lt;br /&gt;
a*	Zero or more of a&lt;br /&gt;
a+	One or more of a&lt;br /&gt;
a{3}	Exactly 3 of a&lt;br /&gt;
a{3,}	3 or more of a&lt;br /&gt;
a{3,6}	Between 3 and 6 of a&lt;br /&gt;
.	Any single character&lt;br /&gt;
\s	Any whitespace character&lt;br /&gt;
\S	Any non-whitespace character&lt;br /&gt;
\d	Any digit&lt;br /&gt;
\D	Any non-digit&lt;br /&gt;
\w	Any word character (letter, number, underscore)&lt;br /&gt;
\W	Any non-word character&lt;br /&gt;
\b	Any word boundary&lt;br /&gt;
[abc]	A single character of: a, b or c&lt;br /&gt;
[^abc]	Any single character except: a, b, or c&lt;br /&gt;
[a-z]	Any single character in the range a-z&lt;br /&gt;
[a-zA-Z]	Any single character in the range a-z or A-Z&lt;br /&gt;
^	Start of line&lt;br /&gt;
$	End of line&lt;br /&gt;
\A	Start of string&lt;br /&gt;
\z	End of string&lt;br /&gt;
options: i case insensitive m make dot match newlines x ignore whitespace in regex &lt;br /&gt;
o perform #{...} substitutions only once&lt;br /&gt;
&lt;br /&gt;
More Examples &lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
/([Rr])uby&amp;amp;\1ails/	Match ruby&amp;amp;rails or Ruby&amp;amp;Rails&lt;br /&gt;
/(['&amp;quot;])(?:(?!\1).)*\1/	Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.&lt;br /&gt;
&lt;br /&gt;
Example	Description&lt;br /&gt;
/^Ruby/	Match &amp;quot;Ruby&amp;quot; at the start of a string or internal line&lt;br /&gt;
/Ruby$/	Match &amp;quot;Ruby&amp;quot; at the end of a string or line&lt;br /&gt;
/\ARuby/	Match &amp;quot;Ruby&amp;quot; at the start of a string&lt;br /&gt;
/Ruby\Z/	Match &amp;quot;Ruby&amp;quot; at the end of a string&lt;br /&gt;
/\bRuby\b/	Match &amp;quot;Ruby&amp;quot; at a word boundary&lt;br /&gt;
/\brub\B/	\B is nonword boundary: match &amp;quot;rub&amp;quot; in &amp;quot;rube&amp;quot; and &amp;quot;ruby&amp;quot; but not alone&lt;br /&gt;
/Ruby(?=!)/	Match &amp;quot;Ruby&amp;quot;, if followed by an exclamation point&lt;br /&gt;
/Ruby(?!!)/	Match &amp;quot;Ruby&amp;quot;, if not followed by an exclamation point&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
line1 = &amp;quot;Cats are smarter than dogs&amp;quot;;&lt;br /&gt;
line2 = &amp;quot;Dogs also like meat&amp;quot;;&lt;br /&gt;
if ( line1 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line1 starts with Cats&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if ( line2 =~ /Cats(.*)/ )&lt;br /&gt;
  puts &amp;quot;Line2 starts with Dogs&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Line1 starts with Cats&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
text = &amp;quot;rails are rails, really good Ruby on Rails&amp;quot;&lt;br /&gt;
# Change &amp;quot;rails&amp;quot; to &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(&amp;quot;rails&amp;quot;, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
# Capitalize the word &amp;quot;Rails&amp;quot; throughout&lt;br /&gt;
text.gsub!(/\brails\b/, &amp;quot;Rails&amp;quot;)&lt;br /&gt;
puts &amp;quot;#{text}&amp;quot;&lt;br /&gt;
This will produce following result:&lt;br /&gt;
Rails are Rails, really good Ruby on Rails&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
==  Modules and Mixins ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modules ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	Ruby Modules are similar to classes in that they hold a collection of methods,constants and other module and class definitions. &lt;br /&gt;
•	Modules definition is similar to classes just that we use the keyword module instead of the class keyword.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====  How Modules differ from classes? ====&lt;br /&gt;
 &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;
&lt;br /&gt;
==== Uses of modules ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Modules serve two purpose:&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;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
•	Here's an example taken from class notes Class notes of CSC517, NCSU to illustrate modules.&lt;br /&gt;
•	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;
•	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.&lt;br /&gt;
•	The solution to this is use of modules. The window function and border function can go into separate modules respectively.&lt;br /&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;
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;
&lt;br /&gt;
When a program needs to use these modules, it can simply load the two files using the Ruby require statement, and reference the qualified names.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
require 'window' &lt;br /&gt;
require 'border' &lt;br /&gt;
trueTop = Window.top + Border.Top.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mixins ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
&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.&lt;br /&gt;
•	Modules can contain class methods and instance methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== How mixins is different from  #include and multiple inheritance? ====&lt;br /&gt;
&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.&lt;br /&gt;
•	By using mixins the same methods can be added to any number of different classes;regardless of hierarchy.&lt;br /&gt;
•	Mixins corresponds to the usage of interfaces in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Consider the following example taken from class notes Class notes of CSC517, NCSU to illustrate mixins.&lt;br /&gt;
&lt;br /&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;
&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;
&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;
&lt;br /&gt;
Consider another example to understand mixins better.&lt;br /&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;
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;
b.kind&lt;br /&gt;
&amp;gt;&amp;gt;BrandNewClass&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Similar functionality in other OO languages ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Namespace in C++ (similar to modules) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====  Definition =====&lt;br /&gt;
 &lt;br /&gt;
•	In general, a namespace is a container for a set of identifiers (names), and allows the disambiguation of homonym identifiers residing in different namespaces.&lt;br /&gt;
•	Namespaces usually group names based on their functionality. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Steps involved =====&lt;br /&gt;
&lt;br /&gt;
To use C++ namespaces, there are two steps involved:&lt;br /&gt;
•	To uniquely identify a namespace with the keyword namespace.&lt;br /&gt;
•	To access the elements of an identified namespace by applying the using keyword.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
===== Example =====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
namespace  NewOne&lt;br /&gt;
{&lt;br /&gt;
      int p;&lt;br /&gt;
      long q&lt;br /&gt;
}&lt;br /&gt;
•	p and q are normal variables but integrated within the NewOne namespace.  &lt;br /&gt;
•	In order to access this variables from the outside the namespace, we have to use the scope operator ::. &lt;br /&gt;
•	From previous example:&lt;br /&gt;
&lt;br /&gt;
NewOne::p;&lt;br /&gt;
NewOne::q;&lt;br /&gt;
&lt;br /&gt;
A namespace definition can be nested within another namespace definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:&lt;br /&gt;
&lt;br /&gt;
// a namespace with using directive&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
namespace SampleOne&lt;br /&gt;
{&lt;br /&gt;
       float p = 10.34;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
namespace SampleTwo&lt;br /&gt;
{&lt;br /&gt;
       using namespace SampleOne;&lt;br /&gt;
       float q = 77.12;&lt;br /&gt;
       namespace InSampleTwo&lt;br /&gt;
       {&lt;br /&gt;
              float r = 34.725;&lt;br /&gt;
       }&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       // this directive gives you everything declared in SampleTwo&lt;br /&gt;
       using namespace SampleTwo;&lt;br /&gt;
       // this directive gives you only InSampleTwo&lt;br /&gt;
       using namespace SampleTwo::InSampleTwo;&lt;br /&gt;
       // local declaration, take precedence&lt;br /&gt;
       float p = 23.11;&lt;br /&gt;
 &lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;p = &amp;quot;&amp;lt;&amp;lt;p&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;q = &amp;quot;&amp;lt;&amp;lt;q&amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout&amp;lt;&amp;lt;&amp;quot;r = &amp;quot;&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;&lt;br /&gt;
       return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====   Multiple inheritance (C++ only) (similar to mixins) ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
 &lt;br /&gt;
In the following example, classes A, B, and C are direct base classes for the derived class X:&lt;br /&gt;
class A { /* ... */ };&lt;br /&gt;
class B { /* ... */ };&lt;br /&gt;
class C { /* ... */ };&lt;br /&gt;
class X : public A, private B, public C { /* ... */ };&lt;br /&gt;
The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:&lt;br /&gt;
 &lt;br /&gt;
The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by destructors.&lt;br /&gt;
A direct base class cannot appear in the base list of a derived class more than once:&lt;br /&gt;
class B1 { /* ... */ };                   // direct base class&lt;br /&gt;
class D : public B1, private B1 { /* ... */ }; // error&lt;br /&gt;
However, a derived class can inherit an indirect base class more than once, as shown in the following example:&lt;br /&gt;
 &lt;br /&gt;
class L { /* ... */ };                  // indirect base class&lt;br /&gt;
class B2 : public L { /* ... */ };&lt;br /&gt;
class B3 : public L { /* ... */ };&lt;br /&gt;
class D : public B2, public B3 { /* ... */ }; // valid&lt;br /&gt;
•	In the above example, class D inherits the indirect base class L once through class B2 and once through class B3.&lt;br /&gt;
•	However, this may lead to ambiguities because two subobjects of class L exist, and both are accessible through class D. &lt;br /&gt;
•	You can avoid this ambiguity by referring to class L using a qualified class name. For example:&lt;br /&gt;
B2::L&lt;br /&gt;
or&lt;br /&gt;
B3::L.&lt;br /&gt;
You can also avoid this ambiguity by using the base specifier virtual to declare a base class, as described in Derivation (C++ only).&lt;br /&gt;
&lt;br /&gt;
==== Interface (Java) (similar to mixins) ====&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===== Definition =====&lt;br /&gt;
 &lt;br /&gt;
•	A Java interface defines a set of methods but does not implement them. &lt;br /&gt;
•	A class that implements the interface agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior, thereby implementing multiple inheritance.&lt;br /&gt;
&lt;br /&gt;
===== Properties of Interface: =====&lt;br /&gt;
&lt;br /&gt;
•	An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.&lt;br /&gt;
•	Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.&lt;br /&gt;
•	Methods in an interface are implicitly public.&lt;br /&gt;
&lt;br /&gt;
===== Example =====&lt;br /&gt;
&lt;br /&gt;
•	When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. &lt;br /&gt;
•	If a class does not perform all the behaviors of the interface, the class must declare itself as abstract.&lt;br /&gt;
/* File name : Animal.java */&lt;br /&gt;
interface Animal {&lt;br /&gt;
&lt;br /&gt;
	public void eat();&lt;br /&gt;
	public void travel();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
•	Aclass uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration.&lt;br /&gt;
/* File name : MammalInt.java */&lt;br /&gt;
public class MammalInt implements Animal{&lt;br /&gt;
&lt;br /&gt;
   public void eat(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal eats&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public void travel(){&lt;br /&gt;
      System.out.println(&amp;quot;Mammal travels&amp;quot;);&lt;br /&gt;
   } &lt;br /&gt;
&lt;br /&gt;
   public int noOfLegs(){&lt;br /&gt;
      return 0;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   public static void main(String args[]){&lt;br /&gt;
      MammalInt m = new MammalInt();&lt;br /&gt;
      m.eat();&lt;br /&gt;
      m.travel();&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
This would produce following result:&lt;br /&gt;
Mammal eats&lt;br /&gt;
Mammal travels&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Comparable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	The comparable mixin is used by classes whose objects may be ordered.&lt;br /&gt;
•	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. &lt;br /&gt;
•	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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is an example taken from class notes Class notes of CSC517, NCSU to illustrate comparable mixin.&lt;br /&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;
&lt;br /&gt;
We compare two lines on the basis of their lengths. We add the Comparable mixin as follows:&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Spaceship operator ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	&amp;lt;=&amp;gt; returns 1,0, or –1, depending on whether the first argument is 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 the squares of the lengths. Now we can use the Comparable methods on Line objects:&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
&lt;br /&gt;
==  Enumerable ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Definition ===&lt;br /&gt;
&lt;br /&gt;
&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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&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;
&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;
•	To understand the working of Enumerable mixin in class,consider the following example taken from class notes 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;
&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;
&lt;br /&gt;
&lt;br /&gt;
==  Simulating Multiple Inheritance ==&lt;br /&gt;
&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.&lt;br /&gt;
•	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;
•	Given below is the Taggable-string example taken from the 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;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
 &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;
&lt;br /&gt;
=== Advantages of Multiple Inheritance ===&lt;br /&gt;
&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;
•	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;
•	For example, in Eiffel, the library class WINDOW is a subclass of SCREENMAN, RECTANGLE, and TWO_WAY_TREE.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Our classification of objects in everyday life is naturally hierarchical. &lt;br /&gt;
We know that all cats are mammals, and all mammals are animals. &lt;br /&gt;
Smaller classes inherit characteristics from the larger classes to which they belong. &lt;br /&gt;
If all mammals breathe, then all cats breathe.&lt;br /&gt;
&lt;br /&gt;
class Mammal&lt;br /&gt;
def breathe&lt;br /&gt;
puts &amp;quot;inhale and exhale&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Cat&amp;lt;Mammal&lt;br /&gt;
def speak&lt;br /&gt;
puts &amp;quot;Meow&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
tama = Cat.new&lt;br /&gt;
&lt;br /&gt;
tama.breathe&lt;br /&gt;
&amp;gt;&amp;gt; inhale and exhale&lt;br /&gt;
&lt;br /&gt;
tama.speak&lt;br /&gt;
&amp;gt;&amp;gt; Meow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''2. Mix and Match'''&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. &lt;br /&gt;
•	There is a mix and match of super-classes combined into a single sub-class. For example, Mixins help achieve this.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
module B&lt;br /&gt;
  def b1&lt;br /&gt;
    puts &amp;quot;Module B method b1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def b2&lt;br /&gt;
    puts &amp;quot;Module B method 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;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
samp=Sample.new&lt;br /&gt;
samp.a1&lt;br /&gt;
samp.a2&lt;br /&gt;
samp.b1&lt;br /&gt;
samp.b2&lt;br /&gt;
samp.s1&lt;br /&gt;
&amp;gt;&amp;gt; Module A method a1&lt;br /&gt;
Module A method a2&lt;br /&gt;
Module B method b1&lt;br /&gt;
Module B method b2&lt;br /&gt;
Class method s1&lt;br /&gt;
&lt;br /&gt;
'''3. Submodularity'''&lt;br /&gt;
•	Modularity of the sub-parts of the classes is noticed and factored out into subclasses. &lt;br /&gt;
•	This is used when the super-classes are modular and the modularity has to be factored out into subclasses. For example, in a class representing Interest rates, one might factor out FIXED_RATE and VARYING Interest rates.&lt;br /&gt;
&lt;br /&gt;
Example &lt;br /&gt;
class Calc&lt;br /&gt;
  def initialize x,y&lt;br /&gt;
    @x=x&lt;br /&gt;
    @y=y&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def result&lt;br /&gt;
puts &amp;quot;product #{Product.result(@x,@y)}&amp;quot;&lt;br /&gt;
puts &amp;quot;Sum #{Sum.result(@x,@y)}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Product&lt;br /&gt;
  def Product.result x,y&lt;br /&gt;
    x*y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Sum&lt;br /&gt;
&lt;br /&gt;
  def Sum.result x,y&lt;br /&gt;
    x+y&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a= Calc.new 3,4&lt;br /&gt;
a.result&lt;br /&gt;
&amp;gt;&amp;gt; product 12&lt;br /&gt;
Sum 7&lt;br /&gt;
&lt;br /&gt;
'''4. Separation of interface and implementation'''&lt;br /&gt;
•	Interfaces are defined by Abstract Classes. Interfaces contain a group of related method declarations. &lt;br /&gt;
•	The methods are not defined in the Interfaces. Interfaces represents the super-class and the sub-classes inherit the interfaces by implementing the interfaces. &lt;br /&gt;
•	In other words, the subclasses encapsulate the implementation details of the interface.&lt;br /&gt;
•	For example, a Stack class could be created as a subclass of StackInterface and StackImplementation.&lt;br /&gt;
•	In ruby Separation of interface and implementation is implemented as shown below.&lt;br /&gt;
Example:&lt;br /&gt;
class Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    raise NotImplementedError.new(&lt;br /&gt;
              &amp;quot;Method not implemented&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Square &amp;lt; Shape&lt;br /&gt;
  def draw&lt;br /&gt;
    puts &amp;quot;abstract method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
s = Square.new.draw&lt;br /&gt;
&amp;gt;&amp;gt; abstract method&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 === Disadvantages of Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	Programmers use multiple inheritance to increase reusability and consistency in the system.&lt;br /&gt;
•	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. &lt;br /&gt;
•	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;
•	The super-classes may be correct and consistent. &lt;br /&gt;
•	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;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. &lt;br /&gt;
•	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;
Example &lt;br /&gt;
module A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A1&lt;br /&gt;
  include A&lt;br /&gt;
  def a1&lt;br /&gt;
    puts &amp;quot;Module A1 method a1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def a2&lt;br /&gt;
    puts &amp;quot;Module A1 method a2&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Sample &amp;lt; A1&lt;br /&gt;
  include A&lt;br /&gt;
  def s1&lt;br /&gt;
    puts &amp;quot;Class method s1&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Here Sample class uses / mixes unnecessarily repeatedly methods of module A&lt;br /&gt;
&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. &lt;br /&gt;
•	An object may need to execute a method (for example, initialize()) which has been defined in different super-classes. &lt;br /&gt;
•	The method resolution becomes a problem during compile time and can lead to run-time errors.&lt;br /&gt;
Example&lt;br /&gt;
Consider the example TeachingAssistant example of multiple inheritance discussed in csc517 class. Even though the program works fine, consider the situation when, user wants to access to_s method of person / student fter declaring to_s in teaching_assistant class?!&lt;br /&gt;
&lt;br /&gt;
module Person&lt;br /&gt;
  attr_accessor :name&lt;br /&gt;
  attr_accessor :height&lt;br /&gt;
  attr_accessor :weight&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Person[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &amp;quot;, weight =&amp;quot; + weight + &amp;quot;]&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Student&lt;br /&gt;
  include Person&lt;br /&gt;
  attr_accessor :major&lt;br /&gt;
  attr_accessor :hometown&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Student[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;]&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module Employee&lt;br /&gt;
  attr_accessor :title&lt;br /&gt;
  attr_accessor :wage&lt;br /&gt;
  include Person&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;Employee[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage+ &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class TeachingAssistant&lt;br /&gt;
include Student&lt;br /&gt;
include Employee&lt;br /&gt;
  def to_s&lt;br /&gt;
    &amp;quot;TeachingAssistant[&amp;quot; + @name + &amp;quot;, height = &amp;quot; + @height + &lt;br /&gt;
    &amp;quot;, weight = &amp;quot; + weight + &amp;quot;, \n   major = &amp;quot; + major + &lt;br /&gt;
    &amp;quot;, hometown = &amp;quot; + hometown + &amp;quot;, title = &amp;quot; + title + &lt;br /&gt;
    &amp;quot;, wage = &amp;quot; + wage + &amp;quot;]&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
john = TeachingAssistant.new();&lt;br /&gt;
john.name = &amp;quot;John&amp;quot;&lt;br /&gt;
john.height = &amp;quot;6 ft. 2 in.&amp;quot;&lt;br /&gt;
john.weight = &amp;quot;180 lbs.&amp;quot;&lt;br /&gt;
john.major = &amp;quot;CS&amp;quot;&lt;br /&gt;
john.hometown = &amp;quot;Springfield&amp;quot;&lt;br /&gt;
john.title = &amp;quot;busboy&amp;quot;&lt;br /&gt;
john.wage = &amp;quot;$7.25/hr.&amp;quot;&lt;br /&gt;
john.to_s&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 4. Implementation difficulties''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
•	Multiple inheritance can result in increased code complexity and implementation difficulties. &lt;br /&gt;
•	In multiple inheritance classes can be combined in several different ways. It becomes difficult for the programmer to represent different objects. &lt;br /&gt;
•	Finding methods needs a lot of search or redirection along the hierarchy.&lt;br /&gt;
&lt;br /&gt;
Consider the below situation where class E inherits A through B C D, if E wishes to access methods of A, which route will class E follow to inherit the methods of A?&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' 5. Misuse''' &lt;br /&gt;
&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. &lt;br /&gt;
•	Therefore, there is a chance that inheritance is used more often than is needed unnecessarily.&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;
•	ApplePie has-a Apple&lt;br /&gt;
•	ApplePie has-a Cinnamon&lt;br /&gt;
•	The relationship can be a object composition but not inheritance.&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;
&lt;br /&gt;
=== Resolution of Name conflicts or Collisions ===&lt;br /&gt;
&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;
&lt;br /&gt;
====  Compound selectors in Ruby ====&lt;br /&gt;
&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;
In Ruby, we can use Compound Selectors to refer to the method as shown below:&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;
&lt;br /&gt;
==== Renaming in Eiffel ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Eiffel language, naming conflicts are overcome in inherited features by renaming. It contains a rename clause to remove name conflicts. This is illustrated below:&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;
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;
&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;
Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Comparison between Set based language and Protocol based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us compare and contrast between Set based language - object specification and Protocol based language - object specification&lt;br /&gt;
&lt;br /&gt;
Set based language - object specification	Protocol based language - object specification&lt;br /&gt;
First, a class is described which abstracts the features or properties of the object we want to specify.	 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;
After describing the template or class, instances or objects for that class are created to perform the actual work. Classes can be described by Meta-classes.	Multiple instances of that object are obtained by copying or cloning.&lt;br /&gt;
Every object instance for a class is unique and holds its internal values for all the features defined for that class.	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;
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;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Set based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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 &lt;br /&gt;
represents all cars and contains features that all cars should have.&lt;br /&gt;
&lt;br /&gt;
• Then we use that car class to create new car objects or instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in Protocol based language - object specification ===&lt;br /&gt;
&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;
&lt;br /&gt;
==== Example: ====&lt;br /&gt;
&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.&lt;br /&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).&lt;br /&gt;
&lt;br /&gt;
• 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. &lt;br /&gt;
&lt;br /&gt;
• This may evolve the assumption that all cars are sedans and fast.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Extending specific objects ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Ruby as a set-based language ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
• Ruby can act as a set-based language.&lt;br /&gt;
• We can first define classes that form the blueprint or a template.&lt;br /&gt;
• We can then create instances for the classes. These objects contain internal values for the properties present in the class.&lt;br /&gt;
• We can then perform inheritance and define sub-classes, modify or add more functionality and create instances for the sub-classes. &lt;br /&gt;
• The sub-class objects receive messages. If they don't understand, they consult the ancestors in the inheritance hierarchy.&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;
&lt;br /&gt;
=== Ruby as a prototype-based language ===&lt;br /&gt;
&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;
&lt;br /&gt;
 Extending Objects&lt;br /&gt;
&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;
• 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;
• The example below, which is taken from the Class notes of CSC517, NCSU illustrates the concept of extending specific objects.&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;
&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;
Extending Objects - example&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. &lt;br /&gt;
•	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;
&lt;br /&gt;
==  References ==&lt;br /&gt;
&lt;br /&gt;
Class notes of CSC517, NCSU&lt;br /&gt;
[1]&lt;br /&gt;
[2]&lt;br /&gt;
[3]&lt;br /&gt;
[4]&lt;br /&gt;
[5]&lt;br /&gt;
Multiple Inheritance&lt;br /&gt;
[6]&lt;br /&gt;
[7] &lt;br /&gt;
http://www.informatics.susx.ac.uk/research/groups/nlp/datr/datrnode33.html &lt;br /&gt;
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html &lt;br /&gt;
Deadly Diamond of Death&lt;br /&gt;
http://en.wikipedia.org/wiki/Diamond_problem Subclasses and Superclasses&lt;br /&gt;
Mixins&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_%28Java%29 &lt;br /&gt;
Object Composition&lt;br /&gt;
Is-a relatioship&lt;br /&gt;
http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Renaming &lt;br /&gt;
http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html&lt;br /&gt;
http://rubular.com/&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm&lt;br /&gt;
http://www.tenouk.com/Module23.html&lt;br /&gt;
http://en.wikipedia.org/wiki/Interface_(Java)&lt;br /&gt;
http://www.tutorialspoint.com/java/java_interfaces.htm&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  External Links ==&lt;br /&gt;
&lt;br /&gt;
Compile time&lt;br /&gt;
Run time&lt;br /&gt;
http://wiki.answers.com/Q/What_are_the_advantages_of_single_inheritance_over_multiple_inheritance &lt;br /&gt;
http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/multiple.html &lt;br /&gt;
Ruby extend and Include&lt;br /&gt;
Ruby Multiple Inheritance with modules and mixins&lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/abstract.html &lt;br /&gt;
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html &lt;br /&gt;
Abstract types&lt;br /&gt;
http://en.wikipedia.org/wiki/Reusability &lt;br /&gt;
&lt;br /&gt;
==  See Also ==&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-lang.org/en/ &lt;br /&gt;
http://www.ruby-lang.org/en/downloads/ &lt;br /&gt;
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 &lt;br /&gt;
http://rubyonrails.org/ &lt;br /&gt;
Object oriented Programming&lt;br /&gt;
Learn Ruby&lt;br /&gt;
Functional programming&lt;br /&gt;
Imperative Programming&lt;br /&gt;
Interpreted language&lt;br /&gt;
Reflection&lt;br /&gt;
Dynamic language&lt;br /&gt;
Python&lt;br /&gt;
Perl&lt;br /&gt;
Ruby on Rails&lt;br /&gt;
Model View Controller - MVC&lt;br /&gt;
Metaprogramming Ruby: Program Like the Ruby Pros&lt;br /&gt;
Design Patterns in Ruby&lt;br /&gt;
Garbage Collection in Ruby&lt;br /&gt;
Threads in Ruby&lt;br /&gt;
Ruby interface to C modules&lt;br /&gt;
Currying&lt;/div&gt;</summary>
		<author><name>Psivash2</name></author>
	</entry>
</feed>