<?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=Dnyayac</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=Dnyayac"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Dnyayac"/>
	<updated>2026-05-18T06:20:45Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53848</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53848"/>
		<updated>2011-10-21T02:54:25Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''&amp;quot;Lecture 6&amp;quot;'''&lt;br /&gt;
='''Regular Expressions'''=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Regular_expression regular expression] provides a succinct and supple means for specifying and recognizing strings of text, such as particular characters, words, or patterns of characters.&amp;lt;ref name=&amp;quot;defobject&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Regular_expression Definition of Regular Expression]&amp;lt;/ref&amp;gt; Regular expressions are usually referred to by abbreviations like &amp;quot;regex&amp;quot; and &amp;quot;regexp&amp;quot;. The concept of regular expressions was first popularized by utilities provided by Unix distributions. Regular expression processor is a program that interprets a regular expression written in a formal language. It either serves as a parser generator or examines text and identifies parts that match the provided specification.&lt;br /&gt;
&lt;br /&gt;
We can demonstrate the power of regex with the help following list of some specifications that can be expressed using regular expressions:&lt;br /&gt;
*the sequence of characters &amp;quot;far&amp;quot; appearing consecutively in any context, such as in &amp;quot;far&amp;quot;, &amp;quot;farmer&amp;quot;, or &amp;quot;defarge&amp;quot;&lt;br /&gt;
*the sequence of characters &amp;quot;far&amp;quot; occurring in that order with other characters between them, such as in &amp;quot;filmstar&amp;quot;&lt;br /&gt;
*the word &amp;quot;far&amp;quot; when it appears as an isolated word&lt;br /&gt;
*the word &amp;quot;far&amp;quot; when preceded by the word &amp;quot;very&amp;quot; or &amp;quot;little&amp;quot;&lt;br /&gt;
*the word &amp;quot;far&amp;quot; when not preceded by the word &amp;quot;went&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Ruby,  built as a better [http://en.wikipedia.org/wiki/Perl Perl] supports regular expressions as a language feature and its syntax is borrowed from Perl. In Ruby, a regular expression is written in the form of /pattern/modifiers where &amp;quot;pattern&amp;quot; is the regular expression itself, and &amp;quot;modifiers&amp;quot; are a series of characters indicating various options.&amp;lt;ref name=&amp;quot;rubyregex&amp;quot;&amp;gt;[http://www.regular-expressions.info/ruby.html Regex Modifiers]&amp;lt;/ref&amp;gt; The &amp;quot;modifiers&amp;quot; part is optional. &lt;br /&gt;
&lt;br /&gt;
Ruby supports the following modifiers:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Modifier&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| /i&lt;br /&gt;
| makes the regex match case insensitive&lt;br /&gt;
|-&lt;br /&gt;
| /m&lt;br /&gt;
| makes the dot match newlines&lt;br /&gt;
|-&lt;br /&gt;
| /x&lt;br /&gt;
| tells Ruby to ignore whitespace between regex tokens&lt;br /&gt;
|-&lt;br /&gt;
| /o&lt;br /&gt;
| causes any #{...} substitutions in a particular regex literal to be performed just once, the first time it is evaluated. Otherwise, the substitutions will be performed every time the literal generates a Regexp object.&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Since forward slashes delimit the regular expression, any forward slashes that appear in the regex need to be escaped. E.g. the regex 3/4 is written as /3\/4/ in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Search And Replace==&lt;br /&gt;
&lt;br /&gt;
The sub() and gsub() methods of the String class can be used search and replace the first regex match or all regex matches respectively. To use sub and gsub we have to specify the regular expression we want to search for as the first parameter, and the replacement string as the second parameter.&lt;br /&gt;
&lt;br /&gt;
The following statement replaces the first occurrence of 'c' in faculty with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
    faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
    =&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The following statement replaces all occurrences of 'e' in cheese with 'o'&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;cheese&amp;quot;.gsub(/e/, &amp;quot;o&amp;quot;)&lt;br /&gt;
    =&amp;gt; &amp;quot;chooso&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Character Classes==&lt;br /&gt;
A character class is delimited with square brackets ([, ]) and lists characters that may appear at that point in the match. /[dm]/ means d or m, as opposed to /dm/ which means d followed by m. The following examples uses character class.&lt;br /&gt;
&lt;br /&gt;
    /Pr[aeiou]gram/.match(&amp;quot;Program&amp;quot;) #=&amp;gt; #&amp;lt;MatchData &amp;quot;Program&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;anecdote&amp;quot;.sub(/[aeio]/, &amp;quot;u&amp;quot;)&lt;br /&gt;
    =&amp;gt;unecdote&lt;br /&gt;
&lt;br /&gt;
If the first character of a character class is a caret (^) the class is inverted: it matches any character except those named. The following example depicts the use of carat.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;anecdote&amp;quot;.sub(/[^aeio]/, &amp;quot;z&amp;quot;)&lt;br /&gt;
    =&amp;gt;azecdote&lt;br /&gt;
&lt;br /&gt;
In a character class, we can use the hyphen (-). This is a metacharacter denoting an inclusive range of characters. [pqrs] is equivalent to [p-s]. A range can follow another range, so [abcdpqrs] is equivalent to [a-dp-s]. The order in which ranges or individual characters appear inside a character class is irrelevant.&lt;br /&gt;
&lt;br /&gt;
The following line will change the first occurrence of any character in the range from a-y in the given string with z&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;now is the time&amp;quot;.sub(/[a-y]/, &amp;quot;z&amp;quot;)&lt;br /&gt;
    =&amp;gt; &amp;quot;zow is the time&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The following line will change the all occurrences of any character in the range from a-y in the given string with z&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;now is the time&amp;quot;.gsub(/[a-y]/, &amp;quot;z&amp;quot;)&lt;br /&gt;
    =&amp;gt; &amp;quot;zzz zz zzz zzzz&amp;quot;&lt;br /&gt;
The following table explains the the different types of character classes:&amp;lt;ref name=&amp;quot;characterclass&amp;quot;&amp;gt;[http://doc.infosnel.nl/ruby_regular_expressions.html Character Classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot; width = &amp;quot;80%&amp;quot; align = &amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Class&lt;br /&gt;
! Match description&lt;br /&gt;
|-&lt;br /&gt;
| [0-9]&lt;br /&gt;
| Decimal digit character&lt;br /&gt;
|-&lt;br /&gt;
| [^0-9]&lt;br /&gt;
| Not a decimal digit character&lt;br /&gt;
|-&lt;br /&gt;
| [\s\t\r\n\f]	&lt;br /&gt;
| Whitespace character&lt;br /&gt;
|-&lt;br /&gt;
| [^\s\t\r\n\f]&lt;br /&gt;
| Not a whitespace character&lt;br /&gt;
|-&lt;br /&gt;
| [A-Za-z0-9_]&lt;br /&gt;
| Word character (alpha, numeric, and underscore)&lt;br /&gt;
|-&lt;br /&gt;
| [^A-Za-z0-9_]&lt;br /&gt;
| Not a word character&lt;br /&gt;
|-&lt;br /&gt;
| [:alnum:]&lt;br /&gt;
| Alpha numeric ([A-Za-z0-9])&lt;br /&gt;
|-&lt;br /&gt;
| [:alpha:]&lt;br /&gt;
| Uppercase and lowercase letters ([A-Za-z])&lt;br /&gt;
|-&lt;br /&gt;
| [:blank:]&lt;br /&gt;
| Blank or tab character&lt;br /&gt;
|-&lt;br /&gt;
| [:space:]&lt;br /&gt;
| Whitespace characters&lt;br /&gt;
|-&lt;br /&gt;
| [:digit:]&lt;br /&gt;
| Decimal digit characters&lt;br /&gt;
|-&lt;br /&gt;
| [:lower:]&lt;br /&gt;
| Lowercase letters ([a-z])&lt;br /&gt;
|-&lt;br /&gt;
| [:upper:]&lt;br /&gt;
| Uppercase characters&lt;br /&gt;
|-&lt;br /&gt;
| [:print:]&lt;br /&gt;
| Any printable character, including space&lt;br /&gt;
|-&lt;br /&gt;
| [:graph:]&lt;br /&gt;
| Printable characters excluding space&lt;br /&gt;
|-&lt;br /&gt;
| [:punct:]&lt;br /&gt;
| Punctuation characters: any printable character excluding aplhanumeric or space&lt;br /&gt;
|-&lt;br /&gt;
| [:cntrl]&lt;br /&gt;
| Chontrol characters (0x00 to 0x1F and 0x7F)&lt;br /&gt;
|-&lt;br /&gt;
| [:xdigit:]	&lt;br /&gt;
| Hexadecimal digits ([0-9a-fA-F])&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Repetition==&lt;br /&gt;
So far we have seen how to match single characters. We can use repetition metacharacter to specify how many times they need to occur. Such metacharacters are called quantifiers.&lt;br /&gt;
&lt;br /&gt;
* * - Zero or more times&lt;br /&gt;
* + - One or more times&lt;br /&gt;
* ? - Zero or one times (optional)&lt;br /&gt;
* {n} - Exactly n times&lt;br /&gt;
* {n,} - n or more times&lt;br /&gt;
* {,m} - m or less times&lt;br /&gt;
* {n,m} - At least n and at most m times&lt;br /&gt;
&lt;br /&gt;
The following line replaces one or more occurrences of 'o' in choose by 'e'&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;choose&amp;quot;.sub(/o+/, &amp;quot;e&amp;quot;)&lt;br /&gt;
    =&amp;gt; &amp;quot;chese&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The following line replaces zero or more occurrences of 'o' in choose by 'e'&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;choose&amp;quot;.sub(/o?/, &amp;quot;e&amp;quot;)&lt;br /&gt;
    =&amp;gt; &amp;quot;echoose&amp;quot;&lt;br /&gt;
&lt;br /&gt;
='''Modules'''=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to accumulate classes, methods and constants into a single, separately defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that the functionality of the modules can be ''mixed-in'' to the classes.&lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] , a module is defined in the following way :&lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children) whereas modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules &amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/modules_mixins.html Modules] &amp;lt;/ref&amp;gt; can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and hence, they are all visible to each other but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into the picture.&lt;br /&gt;
&lt;br /&gt;
='''Mixins'''=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin &amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins] &amp;lt;/ref&amp;gt; is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'', following which included modules are known as‘mixins’. When we mix the modules into a class , all the objects created from that class will be able to use the instance methods of the mixed-in module as if they were defined in the class.&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once &amp;lt;ref&amp;gt;[http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/ Require / Load / Include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Monday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This module defines a constant called FIRST_DAY and initializes it to &amp;quot;Monday&amp;quot;. It also defines two methods which print the appropriate statement.&lt;br /&gt;
&lt;br /&gt;
In order to include this module into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Monday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Monday  120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
='''Comparable and Enumerable'''=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;=. This is done by mixing the module into the class and defining the &amp;lt;=&amp;gt; method. Once this is done , it is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; compares the receiver against another object and returns -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object respectively. &lt;br /&gt;
&lt;br /&gt;
Assume that we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :side&lt;br /&gt;
   def initialize(side)&lt;br /&gt;
   @side = side&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   side*side&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(5)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Enumerable.html Enumerable] is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces &amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/java/concepts/interface.html Interfaces] &amp;lt;/ref&amp;gt;. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes &amp;lt;ref&amp;gt;[http://www.dotnetfunda.com/articles/article467-abstract-class--explained.aspx Abstract Classes]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “''I am a whinnying horse who can fly and chirp !''” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
* You can separate object characteristics into nonoverlapping sets.&lt;br /&gt;
* Lets you create complex classes using only the characteristics that you need, without a proliferation of base classes.&lt;br /&gt;
&lt;br /&gt;
===Disadvantages===&lt;br /&gt;
&lt;br /&gt;
* Complicates method dispatch and impose additional requirements on an application.&lt;br /&gt;
* It is essential to be aware of dependencies on subclass-superclass ordering, particularly in method selection and slot initialization.&lt;br /&gt;
&lt;br /&gt;
='''Extending Objects'''=&lt;br /&gt;
Ruby can act as a [http://en.wikipedia.org/wiki/Class-based_programming set-based language], using include augments a class definition, and hence adds functionality to all objects of a class. But, Ruby can also act as a [http://en.wikipedia.org/wiki/Prototype-based_programming prototype-based] language. The dynamic nature of Ruby can be truly harnessed in a dynamic execution environment. One of the most powerful features is ability to extend (provide additional features to) any object at runtime. This is particularly helpful if we don’t want to modify the object’s class (be it Object for example) but the object itself only. &lt;br /&gt;
&lt;br /&gt;
The following example explains this concept&lt;br /&gt;
We define a class Student, this class is for all students.&lt;br /&gt;
 &lt;br /&gt;
   class Student&lt;br /&gt;
        attr_reader :name, :age&lt;br /&gt;
        def initialize(name, age)&lt;br /&gt;
            @name, @age, = name, age&lt;br /&gt;
        end&lt;br /&gt;
        def gets_paycheck?&lt;br /&gt;
            false&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
We create 2 objects of this class, (mihir and deepak), the basic idea is that students are not paid and thus the function gets_paycheck returns false for both the objects. &lt;br /&gt;
&lt;br /&gt;
    mihir = Student.new('Mihir Surani', 22)&lt;br /&gt;
    deepak = Student.new('Deepak Anand', 25)&lt;br /&gt;
    mihir.gets_paycheck?    #=&amp;gt; false&lt;br /&gt;
    deepak.gets_paycheck?   #=&amp;gt; false&lt;br /&gt;
&lt;br /&gt;
Now, Deepak became a TA, so he should receive a paycheck. We do not want modify the class Student since not all students receive paychecks. Thus, we create a new module TA for students who are TAs. The method gets_paycheck of TA returns true. Also, Deepak will now extend the module TA.&lt;br /&gt;
&lt;br /&gt;
    module TA&lt;br /&gt;
        attr_accessor :course   &lt;br /&gt;
        def gets_paycheck?&lt;br /&gt;
            true&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    deepak.extend(TA)&lt;br /&gt;
    deepak.course = &amp;quot;OOLS&amp;quot;&lt;br /&gt;
    mihir.gets_paycheck?    #=&amp;gt; false&lt;br /&gt;
    deepak.gets_paycheck?   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
    mihir.inspect     #=&amp;gt; &amp;quot;#&amp;lt;Student:0x2401e90 @name=\&amp;quot;Mihir Surani\&amp;quot;, @age=22&amp;gt;&amp;quot;&lt;br /&gt;
    deepak.inspect    #=&amp;gt; &amp;quot;#&amp;lt;Student:0x2c8b390 @name=\&amp;quot;Deepak Anand\&amp;quot;, @age=25, @course=\&amp;quot;OOLS\&amp;quot;&amp;gt;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
='''Conclusion'''=&lt;br /&gt;
&lt;br /&gt;
The above wiki-chapter covers all the topics which were introduced as part of Lecture 6. We began by discussing the topic of Regular Expressions and how ruby uses them. We then drifted our interests towards one of the main topics in Ruby: Modules and Mixins. We discuss the implementation details of modules and showed how modules can be mixed in a class and what advantages it provides. &lt;br /&gt;
&lt;br /&gt;
Finally , we introduced the concept of Multiple Inheritance and its advantages and disadvantages. We also discuss how mixins avoid the diamond problem caused by Multiple inheritance. &lt;br /&gt;
&lt;br /&gt;
We then conclude with the topic of extending objects: how and why is it done.&lt;br /&gt;
&lt;br /&gt;
='''References'''=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
='''See Also'''=&lt;br /&gt;
&lt;br /&gt;
1. http://ruby.about.com/od/beginningruby/a/mixin.html&amp;lt;br&amp;gt;&lt;br /&gt;
2. https://www.re-motion.org/blogs/team/2008/02/20/introducing-mixins-finally/ &amp;lt;br&amp;gt;&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;amp;printable=yes &amp;lt;br&amp;gt;&lt;br /&gt;
4. http://csis.pace.edu/~bergin/patterns/multipleinheritance.html&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53847</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53847"/>
		<updated>2011-10-21T02:54:13Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''&amp;quot;Lecture 6&amp;quot;'''&lt;br /&gt;
='''Regular Expressions'''=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Regular_expression regular expression] provides a succinct and supple means for specifying and recognizing strings of text, such as particular characters, words, or patterns of characters.&amp;lt;ref name=&amp;quot;defobject&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Regular_expression Definition of Regular Expression]&amp;lt;/ref&amp;gt; Regular expressions are usually referred to by abbreviations like &amp;quot;regex&amp;quot; and &amp;quot;regexp&amp;quot;. The concept of regular expressions was first popularized by utilities provided by Unix distributions. Regular expression processor is a program that interprets a regular expression written in a formal language. It either serves as a parser generator or examines text and identifies parts that match the provided specification.&lt;br /&gt;
&lt;br /&gt;
We can demonstrate the power of regex with the help following list of some specifications that can be expressed using regular expressions:&lt;br /&gt;
*the sequence of characters &amp;quot;far&amp;quot; appearing consecutively in any context, such as in &amp;quot;far&amp;quot;, &amp;quot;farmer&amp;quot;, or &amp;quot;defarge&amp;quot;&lt;br /&gt;
*the sequence of characters &amp;quot;far&amp;quot; occurring in that order with other characters between them, such as in &amp;quot;filmstar&amp;quot;&lt;br /&gt;
*the word &amp;quot;far&amp;quot; when it appears as an isolated word&lt;br /&gt;
*the word &amp;quot;far&amp;quot; when preceded by the word &amp;quot;very&amp;quot; or &amp;quot;little&amp;quot;&lt;br /&gt;
*the word &amp;quot;far&amp;quot; when not preceded by the word &amp;quot;went&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Ruby,  built as a better [http://en.wikipedia.org/wiki/Perl Perl] supports regular expressions as a language feature and its syntax is borrowed from Perl. In Ruby, a regular expression is written in the form of /pattern/modifiers where &amp;quot;pattern&amp;quot; is the regular expression itself, and &amp;quot;modifiers&amp;quot; are a series of characters indicating various options.&amp;lt;ref name=&amp;quot;rubyregex&amp;quot;&amp;gt;[http://www.regular-expressions.info/ruby.html Regex Modifiers]&amp;lt;/ref&amp;gt; The &amp;quot;modifiers&amp;quot; part is optional. &lt;br /&gt;
&lt;br /&gt;
Ruby supports the following modifiers:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Modifier&lt;br /&gt;
! Description&lt;br /&gt;
|-&lt;br /&gt;
| /i&lt;br /&gt;
| makes the regex match case insensitive&lt;br /&gt;
|-&lt;br /&gt;
| /m&lt;br /&gt;
| makes the dot match newlines&lt;br /&gt;
|-&lt;br /&gt;
| /x&lt;br /&gt;
| tells Ruby to ignore whitespace between regex tokens&lt;br /&gt;
|-&lt;br /&gt;
| /o&lt;br /&gt;
| causes any #{...} substitutions in a particular regex literal to be performed just once, the first time it is evaluated. Otherwise, the substitutions will be performed every time the literal generates a Regexp object.&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Since forward slashes delimit the regular expression, any forward slashes that appear in the regex need to be escaped. E.g. the regex 3/4 is written as /3\/4/ in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Search And Replace==&lt;br /&gt;
&lt;br /&gt;
The sub() and gsub() methods of the String class can be used search and replace the first regex match or all regex matches respectively. To use sub and gsub we have to specify the regular expression we want to search for as the first parameter, and the replacement string as the second parameter.&lt;br /&gt;
&lt;br /&gt;
The following statement replaces the first occurrence of 'c' in faculty with &amp;quot;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
    faculty&amp;quot;.sub(/c/, &amp;quot;&amp;quot;)&lt;br /&gt;
    =&amp;gt; &amp;quot;faulty&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The following statement replaces all occurrences of 'e' in cheese with 'o'&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;cheese&amp;quot;.gsub(/e/, &amp;quot;o&amp;quot;)&lt;br /&gt;
    =&amp;gt; &amp;quot;chooso&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Character Classes==&lt;br /&gt;
A character class is delimited with square brackets ([, ]) and lists characters that may appear at that point in the match. /[dm]/ means d or m, as opposed to /dm/ which means d followed by m. The following examples uses character class.&lt;br /&gt;
&lt;br /&gt;
    /Pr[aeiou]gram/.match(&amp;quot;Program&amp;quot;) #=&amp;gt; #&amp;lt;MatchData &amp;quot;Program&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;anecdote&amp;quot;.sub(/[aeio]/, &amp;quot;u&amp;quot;)&lt;br /&gt;
    =&amp;gt;unecdote&lt;br /&gt;
&lt;br /&gt;
If the first character of a character class is a caret (^) the class is inverted: it matches any character except those named. The following example depicts the use of carat.&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;anecdote&amp;quot;.sub(/[^aeio]/, &amp;quot;z&amp;quot;)&lt;br /&gt;
    =&amp;gt;azecdote&lt;br /&gt;
&lt;br /&gt;
In a character class, we can use the hyphen (-). This is a metacharacter denoting an inclusive range of characters. [pqrs] is equivalent to [p-s]. A range can follow another range, so [abcdpqrs] is equivalent to [a-dp-s]. The order in which ranges or individual characters appear inside a character class is irrelevant.&lt;br /&gt;
&lt;br /&gt;
The following line will change the first occurrence of any character in the range from a-y in the given string with z&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;now is the time&amp;quot;.sub(/[a-y]/, &amp;quot;z&amp;quot;)&lt;br /&gt;
    =&amp;gt; &amp;quot;zow is the time&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The following line will change the all occurrences of any character in the range from a-y in the given string with z&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;now is the time&amp;quot;.gsub(/[a-y]/, &amp;quot;z&amp;quot;)&lt;br /&gt;
    =&amp;gt; &amp;quot;zzz zz zzz zzzz&amp;quot;&lt;br /&gt;
The following table explains the the different types of character classes:&amp;lt;ref name=&amp;quot;characterclass&amp;quot;&amp;gt;[http://doc.infosnel.nl/ruby_regular_expressions.html Character Classes]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot; width = &amp;quot;80%&amp;quot; align = &amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Class&lt;br /&gt;
! Match description&lt;br /&gt;
|-&lt;br /&gt;
| [0-9]&lt;br /&gt;
| Decimal digit character&lt;br /&gt;
|-&lt;br /&gt;
| [^0-9]&lt;br /&gt;
| Not a decimal digit character&lt;br /&gt;
|-&lt;br /&gt;
| [\s\t\r\n\f]	&lt;br /&gt;
| Whitespace character&lt;br /&gt;
|-&lt;br /&gt;
| [^\s\t\r\n\f]&lt;br /&gt;
| Not a whitespace character&lt;br /&gt;
|-&lt;br /&gt;
| [A-Za-z0-9_]&lt;br /&gt;
| Word character (alpha, numeric, and underscore)&lt;br /&gt;
|-&lt;br /&gt;
| [^A-Za-z0-9_]&lt;br /&gt;
| Not a word character&lt;br /&gt;
|-&lt;br /&gt;
| [:alnum:]&lt;br /&gt;
| Alpha numeric ([A-Za-z0-9])&lt;br /&gt;
|-&lt;br /&gt;
| [:alpha:]&lt;br /&gt;
| Uppercase and lowercase letters ([A-Za-z])&lt;br /&gt;
|-&lt;br /&gt;
| [:blank:]&lt;br /&gt;
| Blank or tab character&lt;br /&gt;
|-&lt;br /&gt;
| [:space:]&lt;br /&gt;
| Whitespace characters&lt;br /&gt;
|-&lt;br /&gt;
| [:digit:]&lt;br /&gt;
| Decimal digit characters&lt;br /&gt;
|-&lt;br /&gt;
| [:lower:]&lt;br /&gt;
| Lowercase letters ([a-z])&lt;br /&gt;
|-&lt;br /&gt;
| [:upper:]&lt;br /&gt;
| Uppercase characters&lt;br /&gt;
|-&lt;br /&gt;
| [:print:]&lt;br /&gt;
| Any printable character, including space&lt;br /&gt;
|-&lt;br /&gt;
| [:graph:]&lt;br /&gt;
| Printable characters excluding space&lt;br /&gt;
|-&lt;br /&gt;
| [:punct:]&lt;br /&gt;
| Punctuation characters: any printable character excluding aplhanumeric or space&lt;br /&gt;
|-&lt;br /&gt;
| [:cntrl]&lt;br /&gt;
| Chontrol characters (0x00 to 0x1F and 0x7F)&lt;br /&gt;
|-&lt;br /&gt;
| [:xdigit:]	&lt;br /&gt;
| Hexadecimal digits ([0-9a-fA-F])&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Repetition==&lt;br /&gt;
So far we have seen how to match single characters. We can use repetition metacharacter to specify how many times they need to occur. Such metacharacters are called quantifiers.&lt;br /&gt;
&lt;br /&gt;
* * - Zero or more times&lt;br /&gt;
* + - One or more times&lt;br /&gt;
* ? - Zero or one times (optional)&lt;br /&gt;
* {n} - Exactly n times&lt;br /&gt;
* {n,} - n or more times&lt;br /&gt;
* {,m} - m or less times&lt;br /&gt;
* {n,m} - At least n and at most m times&lt;br /&gt;
&lt;br /&gt;
The following line replaces one or more occurrences of 'o' in choose by 'e'&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;choose&amp;quot;.sub(/o+/, &amp;quot;e&amp;quot;)&lt;br /&gt;
    =&amp;gt; &amp;quot;chese&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The following line replaces zero or more occurrences of 'o' in choose by 'e'&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;choose&amp;quot;.sub(/o?/, &amp;quot;e&amp;quot;)&lt;br /&gt;
    =&amp;gt; &amp;quot;echoose&amp;quot;&lt;br /&gt;
&lt;br /&gt;
='''Modules'''=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to accumulate classes, methods and constants into a single, separately defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that the functionality of the modules can be ''mixed-in'' to the classes.&lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] , a module is defined in the following way :&lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children) whereas modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules &amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/modules_mixins.html Modules] &amp;lt;/ref&amp;gt; can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and hence, they are all visible to each other but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into the picture.&lt;br /&gt;
&lt;br /&gt;
='''Mixins'''=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin &amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins] &amp;lt;/ref&amp;gt; is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'', following which included modules are known as‘mixins’. When we mix the modules into a class , all the objects created from that class will be able to use the instance methods of the mixed-in module as if they were defined in the class.&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once &amp;lt;ref&amp;gt;[http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/ Require / Load / Include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Monday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This module defines a constant called FIRST_DAY and initializes it to &amp;quot;Monday&amp;quot;. It also defines two methods which print the appropriate statement.&lt;br /&gt;
&lt;br /&gt;
In order to include this module into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Monday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Monday  120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
='''Comparable and Enumerable'''=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;=. This is done by mixing the module into the class and defining the &amp;lt;=&amp;gt; method. Once this is done , it is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; compares the receiver against another object and returns -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object respectively. &lt;br /&gt;
&lt;br /&gt;
Assume that we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :side&lt;br /&gt;
   def initialize(side)&lt;br /&gt;
   @side = side&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   side*side&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(5)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Enumerable.html Enumerable] is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces &amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/java/concepts/interface.html Interfaces] &amp;lt;/ref&amp;gt;. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes &amp;lt;ref&amp;gt;[http://www.dotnetfunda.com/articles/article467-abstract-class--explained.aspx Abstract Classes]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “''I am a whinnying horse who can fly and chirp !''” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;br /&gt;
&lt;br /&gt;
===Advantages===&lt;br /&gt;
&lt;br /&gt;
* You can separate object characteristics into nonoverlapping sets.&lt;br /&gt;
* Lets you create complex classes using only the characteristics that you need, without a proliferation of base classes.&lt;br /&gt;
&lt;br /&gt;
===Disadvantages===&lt;br /&gt;
&lt;br /&gt;
* Complicates method dispatch and impose additional requirements on an application.&lt;br /&gt;
* It is essential to be aware of dependencies on subclass-superclass ordering, particularly in method selection and slot initialization.&lt;br /&gt;
&lt;br /&gt;
='''Extending Objects'''=&lt;br /&gt;
Ruby can act as a [http://en.wikipedia.org/wiki/Class-based_programming set-based language], using include augments a class definition, and hence adds functionality to all objects of a class. But, Ruby can also act as a [http://en.wikipedia.org/wiki/Prototype-based_programming prototype-based] language. The dynamic nature of Ruby can be truly harnessed in a dynamic execution environment. One of the most powerful features is ability to extend (provide additional features to) any object at runtime. This is particularly helpful if we don’t want to modify the object’s class (be it Object for example) but the object itself only. &lt;br /&gt;
&lt;br /&gt;
The following example explains this concept&lt;br /&gt;
We define a class Student, this class is for all students.&lt;br /&gt;
 &lt;br /&gt;
   class Student&lt;br /&gt;
        attr_reader :name, :age&lt;br /&gt;
        def initialize(name, age)&lt;br /&gt;
            @name, @age, = name, age&lt;br /&gt;
        end&lt;br /&gt;
        def gets_paycheck?&lt;br /&gt;
            false&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
We create 2 objects of this class, (mihir and deepak), the basic idea is that students are not paid and thus the function gets_paycheck returns false for both the objects. &lt;br /&gt;
&lt;br /&gt;
    mihir = Student.new('Mihir Surani', 22)&lt;br /&gt;
    deepak = Student.new('Deepak Anand', 25)&lt;br /&gt;
    mihir.gets_paycheck?    #=&amp;gt; false&lt;br /&gt;
    deepak.gets_paycheck?   #=&amp;gt; false&lt;br /&gt;
&lt;br /&gt;
Now, Deepak became a TA, so he should receive a paycheck. We do not want modify the class Student since not all students receive paychecks. Thus, we create a new module TA for students who are TAs. The method gets_paycheck of TA returns true. Also, Deepak will now extend the module TA.&lt;br /&gt;
&lt;br /&gt;
    module TA&lt;br /&gt;
        attr_accessor :course   &lt;br /&gt;
        def gets_paycheck?&lt;br /&gt;
            true&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    deepak.extend(TA)&lt;br /&gt;
    deepak.course = &amp;quot;OOLS&amp;quot;&lt;br /&gt;
    mihir.gets_paycheck?    #=&amp;gt; false&lt;br /&gt;
    deepak.gets_paycheck?   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
    mihir.inspect     #=&amp;gt; &amp;quot;#&amp;lt;Student:0x2401e90 @name=\&amp;quot;Mihir Surani\&amp;quot;, @age=22&amp;gt;&amp;quot;&lt;br /&gt;
    deepak.inspect    #=&amp;gt; &amp;quot;#&amp;lt;Student:0x2c8b390 @name=\&amp;quot;Deepak Anand\&amp;quot;, @age=25, @course=\&amp;quot;OOLS\&amp;quot;&amp;gt;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
='''Conclusion'''=&lt;br /&gt;
&lt;br /&gt;
The above wiki-chapter covers all the topics which were introduced as part of Lecture 6. We began by discussing the topic of Regular Expressions and how ruby uses them. We then drifted our interests towards one of the main topics in Ruby: Modules and Mixins. We discuss the implementation details of modules and showed how modules can be mixed in a class and what advantages it provides. &lt;br /&gt;
Finally , we introduced the concept of Multiple Inheritance and its advantages and disadvantages. We also discuss how mixins avoid the diamond problem caused by Multiple inheritance. &lt;br /&gt;
We then conclude with the topic of extending objects: how and why is it done.&lt;br /&gt;
&lt;br /&gt;
='''References'''=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
='''See Also'''=&lt;br /&gt;
&lt;br /&gt;
1. http://ruby.about.com/od/beginningruby/a/mixin.html&amp;lt;br&amp;gt;&lt;br /&gt;
2. https://www.re-motion.org/blogs/team/2008/02/20/introducing-mixins-finally/ &amp;lt;br&amp;gt;&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;amp;printable=yes &amp;lt;br&amp;gt;&lt;br /&gt;
4. http://csis.pace.edu/~bergin/patterns/multipleinheritance.html&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53767</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53767"/>
		<updated>2011-10-21T02:01:11Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Regular Expressions=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Regular_expression regular expression] provides a succinct and supple means for specifying and recognizing strings of text, such as particular characters, words, or patterns of characters.&amp;lt;ref name=&amp;quot;defobject&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming Definition of Regular Expression]&amp;lt;/ref&amp;gt; Regular expression are usually referred to by abbreviations like &amp;quot;regex&amp;quot; and &amp;quot;regexp&amp;quot;. The concept of regular expressions was first popularized by utilities provided by Unix distributions. A program, regular expression processor interprets a regular expression written in a formal language. This either serves as a parser generator or examines text and identifies parts that match the provided specification.&lt;br /&gt;
&lt;br /&gt;
Here are examples of specifications that could be expressed in a regular expression:&lt;br /&gt;
*the sequence of characters &amp;quot;car&amp;quot; appearing consecutively in any context, such as in &amp;quot;car&amp;quot;, &amp;quot;cartoon&amp;quot;, or &amp;quot;bicarbonate&amp;quot;&lt;br /&gt;
*the sequence of characters &amp;quot;car&amp;quot; occurring in that order with other characters between them, such as in &amp;quot;Icelander&amp;quot; or &amp;quot;chandler&amp;quot;&lt;br /&gt;
*the word &amp;quot;car&amp;quot; when it appears as an isolated word&lt;br /&gt;
*the word &amp;quot;car&amp;quot; when preceded by the word &amp;quot;blue&amp;quot; or &amp;quot;red&amp;quot;&lt;br /&gt;
*the word &amp;quot;car&amp;quot; when not preceded by the word &amp;quot;motor&amp;quot;&lt;br /&gt;
*a dollar sign immediately followed by one or more digits, and then optionally a period and exactly two more digits (for example, &amp;quot;$100&amp;quot; or &amp;quot;$245.99&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Ruby supports regular expressions as a language feature. In Ruby, a regular expression is written in the form of /pattern/modifiers where &amp;quot;pattern&amp;quot; is the regular expression itself, and &amp;quot;modifiers&amp;quot; are a series of characters indicating various options. The &amp;quot;modifiers&amp;quot; part is optional. This syntax is borrowed from Perl. &lt;br /&gt;
&lt;br /&gt;
Ruby supports the following modifiers:&lt;br /&gt;
&lt;br /&gt;
*/i makes the regex match case insensitive.&lt;br /&gt;
*/m makes the dot match newlines. Ruby indeed uses /m, whereas Perl and many other programming languages use /s for &amp;quot;dot matches newlines&amp;quot;.&lt;br /&gt;
*/x tells Ruby to ignore whitespace between regex tokens.&lt;br /&gt;
*/o causes any #{...} substitutions in a particular regex literal to be performed just once, the first time it is evaluated. Otherwise, the substitutions will be performed every time the literal generates a Regexp object.&lt;br /&gt;
&lt;br /&gt;
You can combine multiple modifiers by stringing them together as in /regex/is.&lt;br /&gt;
&lt;br /&gt;
In Ruby, the caret and dollar always match before and after newlines. Ruby does not have a modifier to change this. Use \A and \Z to match at the start or the end of the string.&lt;br /&gt;
&lt;br /&gt;
Since forward slashes delimit the regular expression, any forward slashes that appear in the regex need to be escaped. E.g. the regex 1/2 is written as /1\/2/ in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Search And Replace==&lt;br /&gt;
&lt;br /&gt;
Use the sub() and gsub() methods of the String class to search-and-replace the first regex match, or all regex matches, respectively, in the string. Specify the regular expression you want to search for as the first parameter, and the replacement string as the second parameter, e.g.: result = subject.gsub(/before/, &amp;quot;after&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
To re-insert the regex match, use \0 in the replacement string. You can use the contents of capturing groups in the replacement string with backreferences \1, \2, \3, etc. Note that numbers escaped with a backslash are treated as octal escapes in double-quoted strings. Octal escapes are processed at the language level, before the sub() function sees the parameter. To prevent this, you need to escape the backslashes in double-quoted strings. So to use the first backreference as the replacement string, either pass '\1' or &amp;quot;\\1&amp;quot;. '\\1' also works.&lt;br /&gt;
&lt;br /&gt;
==Character Classes==&lt;br /&gt;
A character class is delimited with square brackets ([, ]) and lists characters that may appear at that point in the match. /[ab]/ means a or b, as opposed to /ab/ which means a followed by.&lt;br /&gt;
&lt;br /&gt;
    /W[aeiou]rd/.match(&amp;quot;Word&amp;quot;) #=&amp;gt; #&amp;lt;MatchData &amp;quot;Word&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within a character class the hyphen (-) is a metacharacter denoting an inclusive range of characters. [abcd] is equivalent to [a-d]. A range can be followed by another range, so [abcdwxyz] is equivalent to [a-dw-z]. The order in which ranges or individual characters appear inside a character class is irrelevant.&lt;br /&gt;
&lt;br /&gt;
    /[0-9a-f]/.match('9f') #=&amp;gt; #&amp;lt;MatchData &amp;quot;9&amp;quot;&amp;gt;&lt;br /&gt;
    /[9f]/.match('9f')     #=&amp;gt; #&amp;lt;MatchData &amp;quot;9&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the first character of a character class is a caret (^) the class is inverted: it matches any character except those named.&lt;br /&gt;
&lt;br /&gt;
    /[^a-eg-z]/.match('f') #=&amp;gt; #&amp;lt;MatchData &amp;quot;f&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A character class may contain another character class. By itself this isn’t useful because [a-z] describes the same set as [a-z0-9]. However, character classes also support the &amp;amp;&amp;amp; operator which performs set intersection on its arguments. The two can be combined as follows:&lt;br /&gt;
&lt;br /&gt;
    /[a-w&amp;amp;&amp;amp;[^c-g]z]/ # ([a-w] AND ([^c-g] OR z))&lt;br /&gt;
This is equivalent to:&lt;br /&gt;
    /[abh-w]/&lt;br /&gt;
&lt;br /&gt;
==Repetition==&lt;br /&gt;
The constructs described so far match a single character. They can be followed by a repetition metacharacter to specify how many times they need to occur. Such metacharacters are called quantifiers.&lt;br /&gt;
&lt;br /&gt;
* * - Zero or more times&lt;br /&gt;
* + - One or more times&lt;br /&gt;
* ? - Zero or one times (optional)&lt;br /&gt;
* {n} - Exactly n times&lt;br /&gt;
* {n,} - n or more times&lt;br /&gt;
* {,m} - m or less times&lt;br /&gt;
* {n,m} - At least n and at most m times&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    # At least one uppercase character ('H'), at least one lowercase&lt;br /&gt;
    # character ('e'), two 'l' characters, then one 'o'&lt;br /&gt;
    &amp;quot;Hello&amp;quot;.match(/[[:upper:]]+[[:lower:]]+l{2}o/) #=&amp;gt; #&amp;lt;MatchData &amp;quot;Hello&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to accumulate classes, methods and constants into a single, separately defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that the functionality of the modules can be ''mixed-in'' to the classes.&lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] , a module is defined in the following way :&lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children) whereas modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules &amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/modules_mixins.html Modules] &amp;lt;/ref&amp;gt; can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and hence, they are all visible to each other but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into the picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin &amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins] &amp;lt;/ref&amp;gt; is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'', following which included modules are known as‘mixins’. When we mix the modules into a class , all the objects created from that class will be able to use the instance methods of the mixed-in module as if they were defined in the class.&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once &amp;lt;ref&amp;gt;[http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/ Require / Load / Include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Monday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This module defines a constant called FIRST_DAY and initializes it to &amp;quot;Monday&amp;quot;. It also defines two methods which print the appropriate statement.&lt;br /&gt;
&lt;br /&gt;
In order to include this module into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Monday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Monday  120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;=. This is done by mixing the module into the class and defining the &amp;lt;=&amp;gt; method. Once this is done , it is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; compares the receiver against another object and returns -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object respectively. &lt;br /&gt;
&lt;br /&gt;
Assume that we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :side&lt;br /&gt;
   def initialize(side)&lt;br /&gt;
   @side = side&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   side*side&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(5)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Enumerable.html Enumerable] is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces &amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/java/concepts/interface.html Interfaces] &amp;lt;/ref&amp;gt;. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes &amp;lt;ref&amp;gt;[http://www.dotnetfunda.com/articles/article467-abstract-class--explained.aspx Abstract Classes]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “''I am a whinnying horse who can fly and chirp !''” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;br /&gt;
&lt;br /&gt;
='''Extending Objects'''=&lt;br /&gt;
Ruby can act as a [http://en.wikipedia.org/wiki/Class-based_programming set-based language], using include augments a class definition, and hence adds functionality to all objects of a class. But, Ruby can also act as a [http://en.wikipedia.org/wiki/Prototype-based_programming prototype-based] language. The dynamic nature of Ruby can be truly harnessed in a dynamic execution environment. One of the most powerful features is ability to extend (provide additional features to) any object at runtime. This is particularly helpful if we don’t want to modify the object’s class (be it Object for example) but the object itself only. &lt;br /&gt;
&lt;br /&gt;
The following example explains this concept&lt;br /&gt;
We define a class Student, this class is for all students.&lt;br /&gt;
 &lt;br /&gt;
   class Student&lt;br /&gt;
        attr_reader :name, :age&lt;br /&gt;
        def initialize(name, age)&lt;br /&gt;
            @name, @age, = name, age&lt;br /&gt;
        end&lt;br /&gt;
        def gets_paycheck?&lt;br /&gt;
            false&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
We create 2 objects of this class, (mihir and deepak), the basic idea is that students are not paid and thus the function gets_paycheck returns false for both the objects. &lt;br /&gt;
&lt;br /&gt;
    mihir = Student.new('Mihir Surani', 22)&lt;br /&gt;
    deepak = Student.new('Deepak Anand', 25)&lt;br /&gt;
    mihir.gets_paycheck?    #=&amp;gt; false&lt;br /&gt;
    deepak.gets_paycheck?   #=&amp;gt; false&lt;br /&gt;
&lt;br /&gt;
Now, Deepak became a TA, so he should receive a paycheck. We do not want modify the class Student since not all students receive paychecks. Thus, we create a new module TA for students who are TAs. The method gets_paycheck of TA returns true. Also, Deepak will now extend the module TA.&lt;br /&gt;
&lt;br /&gt;
    module TA&lt;br /&gt;
        attr_accessor :course   &lt;br /&gt;
        def gets_paycheck?&lt;br /&gt;
            true&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    deepak.extend(TA)&lt;br /&gt;
    deepak.course = &amp;quot;OOLS&amp;quot;&lt;br /&gt;
    mihir.gets_paycheck?    #=&amp;gt; false&lt;br /&gt;
    deepak.gets_paycheck?   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
    mihir.inspect     #=&amp;gt; &amp;quot;#&amp;lt;Student:0x2401e90 @name=\&amp;quot;Mihir Surani\&amp;quot;, @age=22&amp;gt;&amp;quot;&lt;br /&gt;
    deepak.inspect    #=&amp;gt; &amp;quot;#&amp;lt;Student:0x2c8b390 @name=\&amp;quot;Deepak Anand\&amp;quot;, @age=25, @course=\&amp;quot;OOLS\&amp;quot;&amp;gt;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
='''Conclusion'''=&lt;br /&gt;
&lt;br /&gt;
='''References'''=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
='''See Also'''=&lt;br /&gt;
&lt;br /&gt;
1. http://ruby.about.com/od/beginningruby/a/mixin.html&amp;lt;br&amp;gt;&lt;br /&gt;
2. https://www.re-motion.org/blogs/team/2008/02/20/introducing-mixins-finally/ &amp;lt;br&amp;gt;&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;amp;printable=yes &amp;lt;br&amp;gt;&lt;br /&gt;
4. http://csis.pace.edu/~bergin/patterns/multipleinheritance.html&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53766</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53766"/>
		<updated>2011-10-21T02:00:51Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Regular Expressions=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Regular_expression regular expression] provides a succinct and supple means for specifying and recognizing strings of text, such as particular characters, words, or patterns of characters.&amp;lt;ref name=&amp;quot;defobject&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Object-oriented_programming Definition of Regular Expression]&amp;lt;/ref&amp;gt; Regular expression are usually referred to by abbreviations like &amp;quot;regex&amp;quot; and &amp;quot;regexp&amp;quot;. The concept of regular expressions was first popularized by utilities provided by Unix distributions. A program, regular expression processor interprets a regular expression written in a formal language. This either serves as a parser generator or examines text and identifies parts that match the provided specification.&lt;br /&gt;
&lt;br /&gt;
Here are examples of specifications that could be expressed in a regular expression:&lt;br /&gt;
*the sequence of characters &amp;quot;car&amp;quot; appearing consecutively in any context, such as in &amp;quot;car&amp;quot;, &amp;quot;cartoon&amp;quot;, or &amp;quot;bicarbonate&amp;quot;&lt;br /&gt;
*the sequence of characters &amp;quot;car&amp;quot; occurring in that order with other characters between them, such as in &amp;quot;Icelander&amp;quot; or &amp;quot;chandler&amp;quot;&lt;br /&gt;
*the word &amp;quot;car&amp;quot; when it appears as an isolated word&lt;br /&gt;
*the word &amp;quot;car&amp;quot; when preceded by the word &amp;quot;blue&amp;quot; or &amp;quot;red&amp;quot;&lt;br /&gt;
*the word &amp;quot;car&amp;quot; when not preceded by the word &amp;quot;motor&amp;quot;&lt;br /&gt;
*a dollar sign immediately followed by one or more digits, and then optionally a period and exactly two more digits (for example, &amp;quot;$100&amp;quot; or &amp;quot;$245.99&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Ruby supports regular expressions as a language feature. In Ruby, a regular expression is written in the form of /pattern/modifiers where &amp;quot;pattern&amp;quot; is the regular expression itself, and &amp;quot;modifiers&amp;quot; are a series of characters indicating various options. The &amp;quot;modifiers&amp;quot; part is optional. This syntax is borrowed from Perl. &lt;br /&gt;
&lt;br /&gt;
Ruby supports the following modifiers:&lt;br /&gt;
&lt;br /&gt;
*/i makes the regex match case insensitive.&lt;br /&gt;
*/m makes the dot match newlines. Ruby indeed uses /m, whereas Perl and many other programming languages use /s for &amp;quot;dot matches newlines&amp;quot;.&lt;br /&gt;
*/x tells Ruby to ignore whitespace between regex tokens.&lt;br /&gt;
*/o causes any #{...} substitutions in a particular regex literal to be performed just once, the first time it is evaluated. Otherwise, the substitutions will be performed every time the literal generates a Regexp object.&lt;br /&gt;
&lt;br /&gt;
You can combine multiple modifiers by stringing them together as in /regex/is.&lt;br /&gt;
&lt;br /&gt;
In Ruby, the caret and dollar always match before and after newlines. Ruby does not have a modifier to change this. Use \A and \Z to match at the start or the end of the string.&lt;br /&gt;
&lt;br /&gt;
Since forward slashes delimit the regular expression, any forward slashes that appear in the regex need to be escaped. E.g. the regex 1/2 is written as /1\/2/ in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Search And Replace==&lt;br /&gt;
&lt;br /&gt;
Use the sub() and gsub() methods of the String class to search-and-replace the first regex match, or all regex matches, respectively, in the string. Specify the regular expression you want to search for as the first parameter, and the replacement string as the second parameter, e.g.: result = subject.gsub(/before/, &amp;quot;after&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
To re-insert the regex match, use \0 in the replacement string. You can use the contents of capturing groups in the replacement string with backreferences \1, \2, \3, etc. Note that numbers escaped with a backslash are treated as octal escapes in double-quoted strings. Octal escapes are processed at the language level, before the sub() function sees the parameter. To prevent this, you need to escape the backslashes in double-quoted strings. So to use the first backreference as the replacement string, either pass '\1' or &amp;quot;\\1&amp;quot;. '\\1' also works.&lt;br /&gt;
&lt;br /&gt;
==Character Classes==&lt;br /&gt;
A character class is delimited with square brackets ([, ]) and lists characters that may appear at that point in the match. /[ab]/ means a or b, as opposed to /ab/ which means a followed by.&lt;br /&gt;
&lt;br /&gt;
    /W[aeiou]rd/.match(&amp;quot;Word&amp;quot;) #=&amp;gt; #&amp;lt;MatchData &amp;quot;Word&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within a character class the hyphen (-) is a metacharacter denoting an inclusive range of characters. [abcd] is equivalent to [a-d]. A range can be followed by another range, so [abcdwxyz] is equivalent to [a-dw-z]. The order in which ranges or individual characters appear inside a character class is irrelevant.&lt;br /&gt;
&lt;br /&gt;
    /[0-9a-f]/.match('9f') #=&amp;gt; #&amp;lt;MatchData &amp;quot;9&amp;quot;&amp;gt;&lt;br /&gt;
    /[9f]/.match('9f')     #=&amp;gt; #&amp;lt;MatchData &amp;quot;9&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the first character of a character class is a caret (^) the class is inverted: it matches any character except those named.&lt;br /&gt;
&lt;br /&gt;
    /[^a-eg-z]/.match('f') #=&amp;gt; #&amp;lt;MatchData &amp;quot;f&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A character class may contain another character class. By itself this isn’t useful because [a-z] describes the same set as [a-z0-9]. However, character classes also support the &amp;amp;&amp;amp; operator which performs set intersection on its arguments. The two can be combined as follows:&lt;br /&gt;
&lt;br /&gt;
    /[a-w&amp;amp;&amp;amp;[^c-g]z]/ # ([a-w] AND ([^c-g] OR z))&lt;br /&gt;
This is equivalent to:&lt;br /&gt;
    /[abh-w]/&lt;br /&gt;
&lt;br /&gt;
==Repetition==&lt;br /&gt;
The constructs described so far match a single character. They can be followed by a repetition metacharacter to specify how many times they need to occur. Such metacharacters are called quantifiers.&lt;br /&gt;
&lt;br /&gt;
* * - Zero or more times&lt;br /&gt;
* + - One or more times&lt;br /&gt;
* ? - Zero or one times (optional)&lt;br /&gt;
* {n} - Exactly n times&lt;br /&gt;
* {n,} - n or more times&lt;br /&gt;
* {,m} - m or less times&lt;br /&gt;
* {n,m} - At least n and at most m times&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    # At least one uppercase character ('H'), at least one lowercase&lt;br /&gt;
    # character ('e'), two 'l' characters, then one 'o'&lt;br /&gt;
    &amp;quot;Hello&amp;quot;.match(/[[:upper:]]+[[:lower:]]+l{2}o/) #=&amp;gt; #&amp;lt;MatchData &amp;quot;Hello&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to accumulate classes, methods and constants into a single, separately defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that the functionality of the modules can be ''mixed-in'' to the classes.&lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby programming language] , a module is defined in the following way :&lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children) whereas modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules &amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/modules_mixins.html Modules] &amp;lt;/ref&amp;gt; can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and hence, they are all visible to each other but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into the picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin &amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins] &amp;lt;/ref&amp;gt; is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'', following which included modules are known as‘mixins’. When we mix the modules into a class , all the objects created from that class will be able to use the instance methods of the mixed-in module as if they were defined in the class.&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once &amp;lt;ref&amp;gt;[http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/ Require / Load / Include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Monday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This module defines a constant called FIRST_DAY and initializes it to &amp;quot;Monday&amp;quot;. It also defines two methods which print the appropriate statement.&lt;br /&gt;
&lt;br /&gt;
In order to include this module into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Monday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Monday  120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;=. This is done by mixing the module into the class and defining the &amp;lt;=&amp;gt; method. Once this is done , it is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; compares the receiver against another object and returns -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object respectively. &lt;br /&gt;
&lt;br /&gt;
Assume that we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :side&lt;br /&gt;
   def initialize(side)&lt;br /&gt;
   @side = side&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   side*side&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(5)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Enumerable.html Enumerable] is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces &amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/java/concepts/interface.html Interfaces] &amp;lt;/ref&amp;gt;. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes &amp;lt;ref&amp;gt;[http://www.dotnetfunda.com/articles/article467-abstract-class--explained.aspx Abstract Classes]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “''I am a whinnying horse who can fly and chirp !''” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;br /&gt;
&lt;br /&gt;
='''Extending Objects'''=&lt;br /&gt;
Ruby can act as a [http://en.wikipedia.org/wiki/Class-based_programming set-based language], using include augments a class definition, and hence adds functionality to all objects of a class. But, Ruby can also act as a [http://en.wikipedia.org/wiki/Prototype-based_programming prototype-based] language. The dynamic nature of Ruby can be truly harnessed in a dynamic execution environment. One of the most powerful features is ability to extend (provide additional features to) any object at runtime. This is particularly helpful if we don’t want to modify the object’s class (be it Object for example) but the object itself only. &lt;br /&gt;
&lt;br /&gt;
The following example explains this concept&lt;br /&gt;
We define a class Student, this class is for all students.&lt;br /&gt;
 &lt;br /&gt;
   class Student&lt;br /&gt;
        attr_reader :name, :age&lt;br /&gt;
        def initialize(name, age)&lt;br /&gt;
            @name, @age, = name, age&lt;br /&gt;
        end&lt;br /&gt;
        def gets_paycheck?&lt;br /&gt;
            false&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
We create 2 objects of this class, (mihir and deepak), the basic idea is that students are not paid and thus the function gets_paycheck returns false for both the objects. &lt;br /&gt;
&lt;br /&gt;
    mihir = Student.new('Mihir Surani', 22)&lt;br /&gt;
    deepak = Student.new('Deepak Anand', 25)&lt;br /&gt;
    mihir.gets_paycheck?    #=&amp;gt; false&lt;br /&gt;
    deepak.gets_paycheck?   #=&amp;gt; false&lt;br /&gt;
&lt;br /&gt;
Now, Deepak became a TA, so he should receive a paycheck. We do not want modify the class Student since not all students receive paychecks. Thus, we create a new module TA for students who are TAs. The method gets_paycheck of TA returns true. Also, Deepak will now extend the module TA.&lt;br /&gt;
&lt;br /&gt;
    module TA&lt;br /&gt;
        attr_accessor :course   &lt;br /&gt;
        def gets_paycheck?&lt;br /&gt;
            true&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    deepak.extend(TA)&lt;br /&gt;
    deepak.course = &amp;quot;OOLS&amp;quot;&lt;br /&gt;
    mihir.gets_paycheck?    #=&amp;gt; false&lt;br /&gt;
    deepak.gets_paycheck?   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
    mihir.inspect     #=&amp;gt; &amp;quot;#&amp;lt;Student:0x2401e90 @name=\&amp;quot;Mihir Surani\&amp;quot;, @age=22&amp;gt;&amp;quot;&lt;br /&gt;
    deepak.inspect    #=&amp;gt; &amp;quot;#&amp;lt;Student:0x2c8b390 @name=\&amp;quot;Deepak Anand\&amp;quot;, @age=25, @course=\&amp;quot;OOLS\&amp;quot;&amp;gt;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
='''Conclusion'''=&lt;br /&gt;
&lt;br /&gt;
='''References'''=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
='''See Also'''=&lt;br /&gt;
&lt;br /&gt;
1. http://ruby.about.com/od/beginningruby/a/mixin.html&amp;lt;br&amp;gt;&lt;br /&gt;
2. https://www.re-motion.org/blogs/team/2008/02/20/introducing-mixins-finally/ &amp;lt;br&amp;gt;&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;amp;printable=yes &amp;lt;br&amp;gt;&lt;br /&gt;
4. http://csis.pace.edu/~bergin/patterns/multipleinheritance.html&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53176</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53176"/>
		<updated>2011-10-20T19:27:13Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Character Classes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Regular Expressions=&lt;br /&gt;
In computing, a regular expression provides a concise and flexible means for &amp;quot;matching&amp;quot; (specifying and recognizing) strings of text, such as particular characters, words, or patterns of characters. Abbreviations for &amp;quot;regular expression&amp;quot; include &amp;quot;regex&amp;quot; and &amp;quot;regexp&amp;quot;. The concept of regular expressions was first popularized by utilities provided by Unix distributions, in particular the editor ed and the filter grep.[citation needed] A regular expression is written in a formal language that can be interpreted by a regular expression processor, which is a program that either serves as a parser generator or examines text and identifies parts that match the provided specification. Historically, the concept of regular expressions is associated with Kleene's formalism of regular sets, introduced in 1950s.&lt;br /&gt;
&lt;br /&gt;
Here are examples of specifications that could be expressed in a regular expression:&lt;br /&gt;
*the sequence of characters &amp;quot;car&amp;quot; appearing consecutively in any context, such as in &amp;quot;car&amp;quot;, &amp;quot;cartoon&amp;quot;, or &amp;quot;bicarbonate&amp;quot;&lt;br /&gt;
*the sequence of characters &amp;quot;car&amp;quot; occurring in that order with other characters between them, such as in &amp;quot;Icelander&amp;quot; or &amp;quot;chandler&amp;quot;&lt;br /&gt;
*the word &amp;quot;car&amp;quot; when it appears as an isolated word&lt;br /&gt;
*the word &amp;quot;car&amp;quot; when preceded by the word &amp;quot;blue&amp;quot; or &amp;quot;red&amp;quot;&lt;br /&gt;
*the word &amp;quot;car&amp;quot; when not preceded by the word &amp;quot;motor&amp;quot;&lt;br /&gt;
*a dollar sign immediately followed by one or more digits, and then optionally a period and exactly two more digits (for example, &amp;quot;$100&amp;quot; or &amp;quot;$245.99&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Ruby supports regular expressions as a language feature. In Ruby, a regular expression is written in the form of /pattern/modifiers where &amp;quot;pattern&amp;quot; is the regular expression itself, and &amp;quot;modifiers&amp;quot; are a series of characters indicating various options. The &amp;quot;modifiers&amp;quot; part is optional. This syntax is borrowed from Perl. &lt;br /&gt;
&lt;br /&gt;
Ruby supports the following modifiers:&lt;br /&gt;
&lt;br /&gt;
*/i makes the regex match case insensitive.&lt;br /&gt;
*/m makes the dot match newlines. Ruby indeed uses /m, whereas Perl and many other programming languages use /s for &amp;quot;dot matches newlines&amp;quot;.&lt;br /&gt;
*/x tells Ruby to ignore whitespace between regex tokens.&lt;br /&gt;
*/o causes any #{...} substitutions in a particular regex literal to be performed just once, the first time it is evaluated. Otherwise, the substitutions will be performed every time the literal generates a Regexp object.&lt;br /&gt;
&lt;br /&gt;
You can combine multiple modifiers by stringing them together as in /regex/is.&lt;br /&gt;
&lt;br /&gt;
In Ruby, the caret and dollar always match before and after newlines. Ruby does not have a modifier to change this. Use \A and \Z to match at the start or the end of the string.&lt;br /&gt;
&lt;br /&gt;
Since forward slashes delimit the regular expression, any forward slashes that appear in the regex need to be escaped. E.g. the regex 1/2 is written as /1\/2/ in Ruby.&lt;br /&gt;
&lt;br /&gt;
==Search And Replace==&lt;br /&gt;
&lt;br /&gt;
Use the sub() and gsub() methods of the String class to search-and-replace the first regex match, or all regex matches, respectively, in the string. Specify the regular expression you want to search for as the first parameter, and the replacement string as the second parameter, e.g.: result = subject.gsub(/before/, &amp;quot;after&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
To re-insert the regex match, use \0 in the replacement string. You can use the contents of capturing groups in the replacement string with backreferences \1, \2, \3, etc. Note that numbers escaped with a backslash are treated as octal escapes in double-quoted strings. Octal escapes are processed at the language level, before the sub() function sees the parameter. To prevent this, you need to escape the backslashes in double-quoted strings. So to use the first backreference as the replacement string, either pass '\1' or &amp;quot;\\1&amp;quot;. '\\1' also works.&lt;br /&gt;
&lt;br /&gt;
==Character Classes==&lt;br /&gt;
A character class is delimited with square brackets ([, ]) and lists characters that may appear at that point in the match. /[ab]/ means a or b, as opposed to /ab/ which means a followed by.&lt;br /&gt;
&lt;br /&gt;
    /W[aeiou]rd/.match(&amp;quot;Word&amp;quot;) #=&amp;gt; #&amp;lt;MatchData &amp;quot;Word&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within a character class the hyphen (-) is a metacharacter denoting an inclusive range of characters. [abcd] is equivalent to [a-d]. A range can be followed by another range, so [abcdwxyz] is equivalent to [a-dw-z]. The order in which ranges or individual characters appear inside a character class is irrelevant.&lt;br /&gt;
&lt;br /&gt;
    /[0-9a-f]/.match('9f') #=&amp;gt; #&amp;lt;MatchData &amp;quot;9&amp;quot;&amp;gt;&lt;br /&gt;
    /[9f]/.match('9f')     #=&amp;gt; #&amp;lt;MatchData &amp;quot;9&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the first character of a character class is a caret (^) the class is inverted: it matches any character except those named.&lt;br /&gt;
&lt;br /&gt;
    /[^a-eg-z]/.match('f') #=&amp;gt; #&amp;lt;MatchData &amp;quot;f&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A character class may contain another character class. By itself this isn’t useful because [a-z] describes the same set as [a-z0-9]. However, character classes also support the &amp;amp;&amp;amp; operator which performs set intersection on its arguments. The two can be combined as follows:&lt;br /&gt;
&lt;br /&gt;
    /[a-w&amp;amp;&amp;amp;[^c-g]z]/ # ([a-w] AND ([^c-g] OR z))&lt;br /&gt;
This is equivalent to:&lt;br /&gt;
    /[abh-w]/&lt;br /&gt;
&lt;br /&gt;
==Repetition==&lt;br /&gt;
The constructs described so far match a single character. They can be followed by a repetition metacharacter to specify how many times they need to occur. Such metacharacters are called quantifiers.&lt;br /&gt;
&lt;br /&gt;
* * - Zero or more times&lt;br /&gt;
* + - One or more times&lt;br /&gt;
* ? - Zero or one times (optional)&lt;br /&gt;
* {n} - Exactly n times&lt;br /&gt;
* {n,} - n or more times&lt;br /&gt;
* {,m} - m or less times&lt;br /&gt;
* {n,m} - At least n and at most m times&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    # At least one uppercase character ('H'), at least one lowercase&lt;br /&gt;
    # character ('e'), two 'l' characters, then one 'o'&lt;br /&gt;
    &amp;quot;Hello&amp;quot;.match(/[[:upper:]]+[[:lower:]]+l{2}o/) #=&amp;gt; #&amp;lt;MatchData &amp;quot;Hello&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to accumulate classes, methods and constants into a single, separately defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that the functionality of the modules can be ''mixed-in'' to the classes.&lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way :&lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children) whereas modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules &amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/modules_mixins.html Modules] &amp;lt;/ref&amp;gt; can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and hence, they are all visible to each other but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into the picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin &amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins] &amp;lt;/ref&amp;gt; is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'', following which included modules are known as‘mixins’. When we mix the modules into a class , all the objects created from that class will be able to use the instance methods of the mixed-in module as if they were defined in the class.&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once &amp;lt;ref&amp;gt;[http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/ Require / Load / Include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Monday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This module defines a constant called FIRST_DAY and initializes it to &amp;quot;Monday&amp;quot;. It also defines two methods which print the appropriate statement.&lt;br /&gt;
&lt;br /&gt;
In order to include this module into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Monday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Monday  120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;=. This is done by mixing the module into the class and defining the &amp;lt;=&amp;gt; method. Once this is done , it is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; compares the receiver against another object and returns -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object respectively. &lt;br /&gt;
&lt;br /&gt;
Assume that we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :side&lt;br /&gt;
   def initialize(side)&lt;br /&gt;
   @side = side&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   side*side&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(5)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Enumerable.html Enumerable] is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces &amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/java/concepts/interface.html Interfaces] &amp;lt;/ref&amp;gt;. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes &amp;lt;ref&amp;gt;[http://www.dotnetfunda.com/articles/article467-abstract-class--explained.aspx Abstract Classes]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “''I am a whinnying horse who can fly and chirp !''” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
1. http://ruby.about.com/od/beginningruby/a/mixin.html&amp;lt;br&amp;gt;&lt;br /&gt;
2. https://www.re-motion.org/blogs/team/2008/02/20/introducing-mixins-finally/ &amp;lt;br&amp;gt;&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;amp;printable=yes &amp;lt;br&amp;gt;&lt;br /&gt;
4. http://csis.pace.edu/~bergin/patterns/multipleinheritance.html&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53167</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53167"/>
		<updated>2011-10-20T19:22:03Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Comparable */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Regular Expressions=&lt;br /&gt;
In computing, a regular expression provides a concise and flexible means for &amp;quot;matching&amp;quot; (specifying and recognizing) strings of text, such as particular characters, words, or patterns of characters. Abbreviations for &amp;quot;regular expression&amp;quot; include &amp;quot;regex&amp;quot; and &amp;quot;regexp&amp;quot;. The concept of regular expressions was first popularized by utilities provided by Unix distributions, in particular the editor ed and the filter grep.[citation needed] A regular expression is written in a formal language that can be interpreted by a regular expression processor, which is a program that either serves as a parser generator or examines text and identifies parts that match the provided specification. Historically, the concept of regular expressions is associated with Kleene's formalism of regular sets, introduced in 1950s.&lt;br /&gt;
&lt;br /&gt;
Here are examples of specifications that could be expressed in a regular expression:&lt;br /&gt;
*the sequence of characters &amp;quot;car&amp;quot; appearing consecutively in any context, such as in &amp;quot;car&amp;quot;, &amp;quot;cartoon&amp;quot;, or &amp;quot;bicarbonate&amp;quot;&lt;br /&gt;
*the sequence of characters &amp;quot;car&amp;quot; occurring in that order with other characters between them, such as in &amp;quot;Icelander&amp;quot; or &amp;quot;chandler&amp;quot;&lt;br /&gt;
*the word &amp;quot;car&amp;quot; when it appears as an isolated word&lt;br /&gt;
*the word &amp;quot;car&amp;quot; when preceded by the word &amp;quot;blue&amp;quot; or &amp;quot;red&amp;quot;&lt;br /&gt;
*the word &amp;quot;car&amp;quot; when not preceded by the word &amp;quot;motor&amp;quot;&lt;br /&gt;
*a dollar sign immediately followed by one or more digits, and then optionally a period and exactly two more digits (for example, &amp;quot;$100&amp;quot; or &amp;quot;$245.99&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Ruby supports regular expressions as a language feature. In Ruby, a regular expression is written in the form of /pattern/modifiers where &amp;quot;pattern&amp;quot; is the regular expression itself, and &amp;quot;modifiers&amp;quot; are a series of characters indicating various options. The &amp;quot;modifiers&amp;quot; part is optional. This syntax is borrowed from Perl. &lt;br /&gt;
&lt;br /&gt;
Ruby supports the following modifiers:&lt;br /&gt;
&lt;br /&gt;
*/i makes the regex match case insensitive.&lt;br /&gt;
*/m makes the dot match newlines. Ruby indeed uses /m, whereas Perl and many other programming languages use /s for &amp;quot;dot matches newlines&amp;quot;.&lt;br /&gt;
*/x tells Ruby to ignore whitespace between regex tokens.&lt;br /&gt;
*/o causes any #{...} substitutions in a particular regex literal to be performed just once, the first time it is evaluated. Otherwise, the substitutions will be performed every time the literal generates a Regexp object.&lt;br /&gt;
&lt;br /&gt;
You can combine multiple modifiers by stringing them together as in /regex/is.&lt;br /&gt;
&lt;br /&gt;
In Ruby, the caret and dollar always match before and after newlines. Ruby does not have a modifier to change this. Use \A and \Z to match at the start or the end of the string.&lt;br /&gt;
&lt;br /&gt;
Since forward slashes delimit the regular expression, any forward slashes that appear in the regex need to be escaped. E.g. the regex 1/2 is written as /1\/2/ in Ruby.&lt;br /&gt;
&lt;br /&gt;
===Search And Replace===&lt;br /&gt;
&lt;br /&gt;
Use the sub() and gsub() methods of the String class to search-and-replace the first regex match, or all regex matches, respectively, in the string. Specify the regular expression you want to search for as the first parameter, and the replacement string as the second parameter, e.g.: result = subject.gsub(/before/, &amp;quot;after&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
To re-insert the regex match, use \0 in the replacement string. You can use the contents of capturing groups in the replacement string with backreferences \1, \2, \3, etc. Note that numbers escaped with a backslash are treated as octal escapes in double-quoted strings. Octal escapes are processed at the language level, before the sub() function sees the parameter. To prevent this, you need to escape the backslashes in double-quoted strings. So to use the first backreference as the replacement string, either pass '\1' or &amp;quot;\\1&amp;quot;. '\\1' also works.&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to accumulate classes, methods and constants into a single, separately defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that the functionality of the modules can be ''mixed-in'' to the classes.&lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way :&lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children) whereas modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules &amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/modules_mixins.html Modules] &amp;lt;/ref&amp;gt; can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and hence, they are all visible to each other but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into the picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin &amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins] &amp;lt;/ref&amp;gt; is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'', following which included modules are known as‘mixins’. When we mix the modules into a class , all the objects created from that class will be able to use the instance methods of the mixed-in module as if they were defined in the class.&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once &amp;lt;ref&amp;gt;[http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/ Require / Load / Include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Monday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This module defines a constant called FIRST_DAY and initializes it to &amp;quot;Monday&amp;quot;. It also defines two methods which print the appropriate statement.&lt;br /&gt;
&lt;br /&gt;
In order to include this module into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Monday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Monday  120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;=. This is done by mixing the module into the class and defining the &amp;lt;=&amp;gt; method. Once this is done , it is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; compares the receiver against another object and returns -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object respectively. &lt;br /&gt;
&lt;br /&gt;
Assume that we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :side&lt;br /&gt;
   def initialize(side)&lt;br /&gt;
   @side = side&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   side*side&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(5)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Enumerable.html Enumerable] is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces &amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/java/concepts/interface.html Interfaces] &amp;lt;/ref&amp;gt;. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes &amp;lt;ref&amp;gt;[http://www.dotnetfunda.com/articles/article467-abstract-class--explained.aspx Abstract Classes]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “''I am a whinnying horse who can fly and chirp !''” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
1. http://ruby.about.com/od/beginningruby/a/mixin.html&amp;lt;br&amp;gt;&lt;br /&gt;
2. https://www.re-motion.org/blogs/team/2008/02/20/introducing-mixins-finally/ &amp;lt;br&amp;gt;&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;amp;printable=yes &amp;lt;br&amp;gt;&lt;br /&gt;
4. http://csis.pace.edu/~bergin/patterns/multipleinheritance.html&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53164</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53164"/>
		<updated>2011-10-20T19:18:48Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Regular Expressions=&lt;br /&gt;
In computing, a regular expression provides a concise and flexible means for &amp;quot;matching&amp;quot; (specifying and recognizing) strings of text, such as particular characters, words, or patterns of characters. Abbreviations for &amp;quot;regular expression&amp;quot; include &amp;quot;regex&amp;quot; and &amp;quot;regexp&amp;quot;. The concept of regular expressions was first popularized by utilities provided by Unix distributions, in particular the editor ed and the filter grep.[citation needed] A regular expression is written in a formal language that can be interpreted by a regular expression processor, which is a program that either serves as a parser generator or examines text and identifies parts that match the provided specification. Historically, the concept of regular expressions is associated with Kleene's formalism of regular sets, introduced in 1950s.&lt;br /&gt;
&lt;br /&gt;
Here are examples of specifications that could be expressed in a regular expression:&lt;br /&gt;
*the sequence of characters &amp;quot;car&amp;quot; appearing consecutively in any context, such as in &amp;quot;car&amp;quot;, &amp;quot;cartoon&amp;quot;, or &amp;quot;bicarbonate&amp;quot;&lt;br /&gt;
*the sequence of characters &amp;quot;car&amp;quot; occurring in that order with other characters between them, such as in &amp;quot;Icelander&amp;quot; or &amp;quot;chandler&amp;quot;&lt;br /&gt;
*the word &amp;quot;car&amp;quot; when it appears as an isolated word&lt;br /&gt;
*the word &amp;quot;car&amp;quot; when preceded by the word &amp;quot;blue&amp;quot; or &amp;quot;red&amp;quot;&lt;br /&gt;
*the word &amp;quot;car&amp;quot; when not preceded by the word &amp;quot;motor&amp;quot;&lt;br /&gt;
*a dollar sign immediately followed by one or more digits, and then optionally a period and exactly two more digits (for example, &amp;quot;$100&amp;quot; or &amp;quot;$245.99&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Ruby supports regular expressions as a language feature. In Ruby, a regular expression is written in the form of /pattern/modifiers where &amp;quot;pattern&amp;quot; is the regular expression itself, and &amp;quot;modifiers&amp;quot; are a series of characters indicating various options. The &amp;quot;modifiers&amp;quot; part is optional. This syntax is borrowed from Perl. &lt;br /&gt;
&lt;br /&gt;
Ruby supports the following modifiers:&lt;br /&gt;
&lt;br /&gt;
*/i makes the regex match case insensitive.&lt;br /&gt;
*/m makes the dot match newlines. Ruby indeed uses /m, whereas Perl and many other programming languages use /s for &amp;quot;dot matches newlines&amp;quot;.&lt;br /&gt;
*/x tells Ruby to ignore whitespace between regex tokens.&lt;br /&gt;
*/o causes any #{...} substitutions in a particular regex literal to be performed just once, the first time it is evaluated. Otherwise, the substitutions will be performed every time the literal generates a Regexp object.&lt;br /&gt;
&lt;br /&gt;
You can combine multiple modifiers by stringing them together as in /regex/is.&lt;br /&gt;
&lt;br /&gt;
In Ruby, the caret and dollar always match before and after newlines. Ruby does not have a modifier to change this. Use \A and \Z to match at the start or the end of the string.&lt;br /&gt;
&lt;br /&gt;
Since forward slashes delimit the regular expression, any forward slashes that appear in the regex need to be escaped. E.g. the regex 1/2 is written as /1\/2/ in Ruby.&lt;br /&gt;
&lt;br /&gt;
===Search And Replace===&lt;br /&gt;
&lt;br /&gt;
Use the sub() and gsub() methods of the String class to search-and-replace the first regex match, or all regex matches, respectively, in the string. Specify the regular expression you want to search for as the first parameter, and the replacement string as the second parameter, e.g.: result = subject.gsub(/before/, &amp;quot;after&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
To re-insert the regex match, use \0 in the replacement string. You can use the contents of capturing groups in the replacement string with backreferences \1, \2, \3, etc. Note that numbers escaped with a backslash are treated as octal escapes in double-quoted strings. Octal escapes are processed at the language level, before the sub() function sees the parameter. To prevent this, you need to escape the backslashes in double-quoted strings. So to use the first backreference as the replacement string, either pass '\1' or &amp;quot;\\1&amp;quot;. '\\1' also works.&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to accumulate classes, methods and constants into a single, separately defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that the functionality of the modules can be ''mixed-in'' to the classes.&lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way :&lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children) whereas modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules &amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/modules_mixins.html Modules] &amp;lt;/ref&amp;gt; can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and hence, they are all visible to each other but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into the picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin &amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins] &amp;lt;/ref&amp;gt; is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'', following which included modules are known as‘mixins’. When we mix the modules into a class , all the objects created from that class will be able to use the instance methods of the mixed-in module as if they were defined in the class.&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once &amp;lt;ref&amp;gt;[http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/ Require / Load / Include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Monday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This module defines a constant called FIRST_DAY and initializes it to &amp;quot;Monday&amp;quot;. It also defines two methods which print the appropriate statement.&lt;br /&gt;
&lt;br /&gt;
In order to include this module into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Monday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Monday  120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; 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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Enumerable.html Enumerable] is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces &amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/java/concepts/interface.html Interfaces] &amp;lt;/ref&amp;gt;. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes &amp;lt;ref&amp;gt;[http://www.dotnetfunda.com/articles/article467-abstract-class--explained.aspx Abstract Classes]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “''I am a whinnying horse who can fly and chirp !''” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
1. http://ruby.about.com/od/beginningruby/a/mixin.html&amp;lt;br&amp;gt;&lt;br /&gt;
2. https://www.re-motion.org/blogs/team/2008/02/20/introducing-mixins-finally/ &amp;lt;br&amp;gt;&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;amp;printable=yes &amp;lt;br&amp;gt;&lt;br /&gt;
4. http://csis.pace.edu/~bergin/patterns/multipleinheritance.html&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53159</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53159"/>
		<updated>2011-10-20T19:07:03Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Regular Expressions=&lt;br /&gt;
In computing, a regular expression provides a concise and flexible means for &amp;quot;matching&amp;quot; (specifying and recognizing) strings of text, such as particular characters, words, or patterns of characters. Abbreviations for &amp;quot;regular expression&amp;quot; include &amp;quot;regex&amp;quot; and &amp;quot;regexp&amp;quot;. The concept of regular expressions was first popularized by utilities provided by Unix distributions, in particular the editor ed and the filter grep.[citation needed] A regular expression is written in a formal language that can be interpreted by a regular expression processor, which is a program that either serves as a parser generator or examines text and identifies parts that match the provided specification. Historically, the concept of regular expressions is associated with Kleene's formalism of regular sets, introduced in 1950s.&lt;br /&gt;
&lt;br /&gt;
Here are examples of specifications that could be expressed in a regular expression:&lt;br /&gt;
*the sequence of characters &amp;quot;car&amp;quot; appearing consecutively in any context, such as in &amp;quot;car&amp;quot;, &amp;quot;cartoon&amp;quot;, or &amp;quot;bicarbonate&amp;quot;&lt;br /&gt;
*the sequence of characters &amp;quot;car&amp;quot; occurring in that order with other characters between them, such as in &amp;quot;Icelander&amp;quot; or &amp;quot;chandler&amp;quot;&lt;br /&gt;
*the word &amp;quot;car&amp;quot; when it appears as an isolated word&lt;br /&gt;
*the word &amp;quot;car&amp;quot; when preceded by the word &amp;quot;blue&amp;quot; or &amp;quot;red&amp;quot;&lt;br /&gt;
*the word &amp;quot;car&amp;quot; when not preceded by the word &amp;quot;motor&amp;quot;&lt;br /&gt;
*a dollar sign immediately followed by one or more digits, and then optionally a period and exactly two more digits (for example, &amp;quot;$100&amp;quot; or &amp;quot;$245.99&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Ruby supports regular expressions as a language feature. In Ruby, a regular expression is written in the form of /pattern/modifiers where &amp;quot;pattern&amp;quot; is the regular expression itself, and &amp;quot;modifiers&amp;quot; are a series of characters indicating various options. The &amp;quot;modifiers&amp;quot; part is optional. This syntax is borrowed from Perl. &lt;br /&gt;
&lt;br /&gt;
Ruby supports the following modifiers:&lt;br /&gt;
&lt;br /&gt;
*/i makes the regex match case insensitive.&lt;br /&gt;
*/m makes the dot match newlines. Ruby indeed uses /m, whereas Perl and many other programming languages use /s for &amp;quot;dot matches newlines&amp;quot;.&lt;br /&gt;
*/x tells Ruby to ignore whitespace between regex tokens.&lt;br /&gt;
*/o causes any #{...} substitutions in a particular regex literal to be performed just once, the first time it is evaluated. Otherwise, the substitutions will be performed every time the literal generates a Regexp object.&lt;br /&gt;
&lt;br /&gt;
You can combine multiple modifiers by stringing them together as in /regex/is.&lt;br /&gt;
&lt;br /&gt;
In Ruby, the caret and dollar always match before and after newlines. Ruby does not have a modifier to change this. Use \A and \Z to match at the start or the end of the string.&lt;br /&gt;
&lt;br /&gt;
Since forward slashes delimit the regular expression, any forward slashes that appear in the regex need to be escaped. E.g. the regex 1/2 is written as /1\/2/ in Ruby.&lt;br /&gt;
&lt;br /&gt;
===Search And Replace===&lt;br /&gt;
&lt;br /&gt;
Use the sub() and gsub() methods of the String class to search-and-replace the first regex match, or all regex matches, respectively, in the string. Specify the regular expression you want to search for as the first parameter, and the replacement string as the second parameter, e.g.: result = subject.gsub(/before/, &amp;quot;after&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
To re-insert the regex match, use \0 in the replacement string. You can use the contents of capturing groups in the replacement string with backreferences \1, \2, \3, etc. Note that numbers escaped with a backslash are treated as octal escapes in double-quoted strings. Octal escapes are processed at the language level, before the sub() function sees the parameter. To prevent this, you need to escape the backslashes in double-quoted strings. So to use the first backreference as the replacement string, either pass '\1' or &amp;quot;\\1&amp;quot;. '\\1' also works.&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to accumulate classes, methods and constants into a single, separately defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that the functionality of the modules can be ''mixed-in'' to the classes.&lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way :&lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children) whereas modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules &amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/modules_mixins.html Modules] &amp;lt;/ref&amp;gt; can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and hence, they are all visible to each other but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into the picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin &amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins] &amp;lt;/ref&amp;gt; is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'' – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once &amp;lt;ref&amp;gt;[http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/ Require / Load / Include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; 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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Enumerable.html Enumerable] is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces &amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/java/concepts/interface.html Interfaces] &amp;lt;/ref&amp;gt;. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes &amp;lt;ref&amp;gt;[http://www.dotnetfunda.com/articles/article467-abstract-class--explained.aspx Abstract Classes]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “''I am a whinnying horse who can fly and chirp !''” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
1. http://ruby.about.com/od/beginningruby/a/mixin.html&amp;lt;br&amp;gt;&lt;br /&gt;
2. https://www.re-motion.org/blogs/team/2008/02/20/introducing-mixins-finally/ &amp;lt;br&amp;gt;&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;amp;printable=yes &amp;lt;br&amp;gt;&lt;br /&gt;
4. http://csis.pace.edu/~bergin/patterns/multipleinheritance.html&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53034</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53034"/>
		<updated>2011-10-20T14:02:23Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Composing modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules &amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/modules_mixins.html Modules] &amp;lt;/ref&amp;gt; can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin &amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins] &amp;lt;/ref&amp;gt; is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'' – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once &amp;lt;ref&amp;gt;[http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/ Require / Load / Include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; 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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
[http://www.ruby-doc.org/core-1.9.2/Enumerable.html Enumerable] is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces &amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/java/concepts/interface.html Interfaces] &amp;lt;/ref&amp;gt;. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes &amp;lt;ref&amp;gt;[http://www.dotnetfunda.com/articles/article467-abstract-class--explained.aspx Abstract Classes]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “''I am a whinnying horse who can fly and chirp !''” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
1. http://ruby.about.com/od/beginningruby/a/mixin.html&amp;lt;br&amp;gt;&lt;br /&gt;
2. https://www.re-motion.org/blogs/team/2008/02/20/introducing-mixins-finally/ &amp;lt;br&amp;gt;&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;amp;printable=yes &amp;lt;br&amp;gt;&lt;br /&gt;
4. http://csis.pace.edu/~bergin/patterns/multipleinheritance.html&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53033</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53033"/>
		<updated>2011-10-20T13:58:30Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Further Reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules &amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/modules_mixins.html Modules] &amp;lt;/ref&amp;gt; can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin &amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins] &amp;lt;/ref&amp;gt; is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'' – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once &amp;lt;ref&amp;gt;[http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/ Require / Load / Include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; 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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces &amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/java/concepts/interface.html Interfaces] &amp;lt;/ref&amp;gt;. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes &amp;lt;ref&amp;gt;[http://www.dotnetfunda.com/articles/article467-abstract-class--explained.aspx Abstract Classes]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “''I am a whinnying horse who can fly and chirp !''” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
&lt;br /&gt;
1. http://ruby.about.com/od/beginningruby/a/mixin.html&amp;lt;br&amp;gt;&lt;br /&gt;
2. https://www.re-motion.org/blogs/team/2008/02/20/introducing-mixins-finally/ &amp;lt;br&amp;gt;&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;amp;printable=yes &amp;lt;br&amp;gt;&lt;br /&gt;
4. http://csis.pace.edu/~bergin/patterns/multipleinheritance.html&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53032</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53032"/>
		<updated>2011-10-20T13:57:13Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules &amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/modules_mixins.html Modules] &amp;lt;/ref&amp;gt; can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin &amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins] &amp;lt;/ref&amp;gt; is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'' – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once &amp;lt;ref&amp;gt;[http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/ Require / Load / Include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; 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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces &amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/java/concepts/interface.html Interfaces] &amp;lt;/ref&amp;gt;. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes &amp;lt;ref&amp;gt;[http://www.dotnetfunda.com/articles/article467-abstract-class--explained.aspx Abstract Classes]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “''I am a whinnying horse who can fly and chirp !''” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53031</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53031"/>
		<updated>2011-10-20T13:54:53Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Multiple Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules &amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/modules_mixins.html Modules] &amp;lt;/ref&amp;gt; can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin &amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins] &amp;lt;/ref&amp;gt; is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'' – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once &amp;lt;ref&amp;gt;[http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/ Require / Load / Include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; 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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces &amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/java/concepts/interface.html Interfaces] &amp;lt;/ref&amp;gt;. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes &amp;lt;ref&amp;gt;[http://www.dotnetfunda.com/articles/article467-abstract-class--explained.aspx Abstract Classes]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “''I am a whinnying horse who can fly and chirp !''” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53030</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53030"/>
		<updated>2011-10-20T13:50:22Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Modules as Namespaces */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules &amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/modules_mixins.html Modules] &amp;lt;/ref&amp;gt; can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin &amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins] &amp;lt;/ref&amp;gt; is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'' – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once &amp;lt;ref&amp;gt;[http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/ Require / Load / Include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; 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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes &amp;lt;ref&amp;gt;[http://www.dotnetfunda.com/articles/article467-abstract-class--explained.aspx Abstract Classes]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “''I am a whinnying horse who can fly and chirp !''” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53029</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53029"/>
		<updated>2011-10-20T13:49:47Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules &amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/modules_mixins.html Modules / Mixins] &amp;lt;/ref&amp;gt; can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin &amp;lt;ref&amp;gt;[http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ Mixins] &amp;lt;/ref&amp;gt; is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'' – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once &amp;lt;ref&amp;gt;[http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/ Require / Load / Include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; 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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes &amp;lt;ref&amp;gt;[http://www.dotnetfunda.com/articles/article467-abstract-class--explained.aspx Abstract Classes]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “''I am a whinnying horse who can fly and chirp !''” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53028</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53028"/>
		<updated>2011-10-20T13:48:41Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules &amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/modules_mixins.html Modules / Mixins] &amp;lt;/ref&amp;gt; can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin &amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/modules_mixins.html Modules / Mixins] &amp;lt;/ref&amp;gt; is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'' – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once &amp;lt;ref&amp;gt;[http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/ Require / Load / Include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; 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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes &amp;lt;ref&amp;gt;[http://www.dotnetfunda.com/articles/article467-abstract-class--explained.aspx Abstract Classes]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “''I am a whinnying horse who can fly and chirp !''” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53027</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53027"/>
		<updated>2011-10-20T13:48:28Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Modules as Namespaces */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules &amp;lt;ref&amp;gt;[http://rubylearning.com/satishtalim/modules_mixins.html Modules / Mixins] &amp;lt;/ref&amp;gt; can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'' – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once &amp;lt;ref&amp;gt;[http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/ Require / Load / Include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; 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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes &amp;lt;ref&amp;gt;[http://www.dotnetfunda.com/articles/article467-abstract-class--explained.aspx Abstract Classes]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “''I am a whinnying horse who can fly and chirp !''” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53026</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53026"/>
		<updated>2011-10-20T13:44:24Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Multiple Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'' – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once &amp;lt;ref&amp;gt;[http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/ Require / Load / Include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; 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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes &amp;lt;ref&amp;gt;[http://www.dotnetfunda.com/articles/article467-abstract-class--explained.aspx Abstract Classes]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “''I am a whinnying horse who can fly and chirp !''” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53025</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53025"/>
		<updated>2011-10-20T13:42:46Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Require / Load */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'' – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once &amp;lt;ref&amp;gt;[http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/ Require / Load / Include]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; 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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes &amp;lt;ref&amp;gt;[http://www.dotnetfunda.com/articles/article467-abstract-class--explained.aspx Abstract Classes]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “I am a whinnying horse who can fly and chirp !” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53022</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53022"/>
		<updated>2011-10-20T13:37:35Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* 4c Chapter 6 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'' – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; 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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes &amp;lt;ref&amp;gt;[http://www.dotnetfunda.com/articles/article467-abstract-class--explained.aspx Abstract Classes]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “I am a whinnying horse who can fly and chirp !” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53020</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53020"/>
		<updated>2011-10-20T13:34:09Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Multiple Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'' – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; 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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes &amp;lt;ref&amp;gt;[http://www.dotnetfunda.com/articles/article467-abstract-class--explained.aspx Abstract Classes]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “I am a whinnying horse who can fly and chirp !” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53019</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53019"/>
		<updated>2011-10-20T13:32:59Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Multiple Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'' – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; 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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes &amp;lt;ref&amp;gt;[http://www.dotnetfunda.com/articles/article467-abstract-class--explained.aspx]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “I am a whinnying horse who can fly and chirp !” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53016</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53016"/>
		<updated>2011-10-20T13:27:41Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Composing modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'' – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; 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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect), this will ‘''behind the scenes''’ call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “I am a whinnying horse who can fly and chirp !” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53014</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53014"/>
		<updated>2011-10-20T13:23:24Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Comparable */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'' – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The &amp;lt;=&amp;gt; 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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function ([http://en.wikipedia.org/wiki/Spaceship_operator spaceship operator]) uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect) this will, ‘behind the scenes’, call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “I am a whinnying horse who can fly and chirp !” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53013</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53013"/>
		<updated>2011-10-20T13:20:39Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Mixins */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ''‘mixing in’'' – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we always need to use ''include''.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect) this will, ‘behind the scenes’, call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “I am a whinnying horse who can fly and chirp !” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53012</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53012"/>
		<updated>2011-10-20T13:19:07Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have [http://en.wikipedia.org/wiki/Instance_(computer_science) instances] (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited ,they provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same [http://en.wikipedia.org/wiki/Namespace ‘namespace’] and so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect) this will, ‘behind the scenes’, call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “I am a whinnying horse who can fly and chirp !” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53011</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53011"/>
		<updated>2011-10-20T13:14:43Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In [http://en.wikipedia.org/wiki/Ruby_(programming_language)Ruby programming language] , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as [http://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods class methods] are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses superclasses] (parents) and [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_superclasses subclasses] (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect) this will, ‘behind the scenes’, call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “I am a whinnying horse who can fly and chirp !” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53010</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53010"/>
		<updated>2011-10-20T13:07:34Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Multiple Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect) this will, ‘behind the scenes’, call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “I am a whinnying horse who can fly and chirp !” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53009</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53009"/>
		<updated>2011-10-20T13:07:03Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Multiple Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect) this will, ‘behind the scenes’, call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Multiple_inheritance Multiple Inheritance] refers to a feature in some [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming languages] in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java_(programming_language) Java] makes use of the concept of Interfaces. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel] makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “I am a whinnying horse who can fly and chirp !” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53007</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53007"/>
		<updated>2011-10-20T13:03:42Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Multiple Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect) this will, ‘behind the scenes’, call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
 Multiple Inheritance refers to a feature in some object oriented programming languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the [http://en.wikipedia.org/wiki/Diamond_problem “diamond problem”].  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
Java makes use of the concept of Interfaces. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
Eiffel makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “I am a whinnying horse who can fly and chirp !” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53005</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53005"/>
		<updated>2011-10-20T13:02:52Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Multiple Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect) this will, ‘behind the scenes’, call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Diamond_problem Multiple Inheritance]refers to a feature in some object oriented programming languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the “diamond problem”.  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
Java makes use of the concept of Interfaces. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
Eiffel makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “I am a whinnying horse who can fly and chirp !” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53001</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53001"/>
		<updated>2011-10-20T13:00:20Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Multiple Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect) this will, ‘behind the scenes’, call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Multiple Inheritance refers to a feature in some object oriented programming languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
One of the ambiguities associated with multiple inheritance is the “diamond problem”.  Different languages have approach this problem in different ways. &lt;br /&gt;
&lt;br /&gt;
Java makes use of the concept of Interfaces. Using interfaces, Java allows multiple inheritance of interfaces but implementation which consists of methods and variables maybe only singly inherited. This way , Java ensure that there is no confusion over which method to call of which instance variable to use. &lt;br /&gt;
&lt;br /&gt;
Eiffel makes use of the renaming clause and ensures that the ancestor methods used in the descendants are explicitly specified. This allows the methods of the base class to be shared between all its descendants.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “I am a whinnying horse who can fly and chirp !” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52972</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52972"/>
		<updated>2011-10-20T06:29:34Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Multiple Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect) this will, ‘behind the scenes’, call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Multiple Inheritance refers to a feature in some object oriented programming languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Horse and Bird, each of which contain the talk method defined in them. The Pegasus class includes these two modules and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “I am a whinnying horse who can fly and chirp !” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52971</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52971"/>
		<updated>2011-10-20T06:28:30Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Multiple Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect) this will, ‘behind the scenes’, call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Multiple Inheritance refers to a feature in some object oriented programming languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;br /&gt;
&lt;br /&gt;
The below code in Ruby demonstrates two modules Frog and Dinosaur, each of which contain the talk method defined in them. The Frogsaur class includes these two methods and defines its own talk method. This example was chosen to exhibit on how Ruby resolves method calls at run time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 module Horse&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Neigh !!!!! &amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  module Bird&lt;br /&gt;
      def talk&lt;br /&gt;
          puts &amp;quot;I can Chirp !!!!!&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
  end&lt;br /&gt;
	&lt;br /&gt;
  class Pegasus&lt;br /&gt;
      include Horse&lt;br /&gt;
      include Bird&lt;br /&gt;
         def talk&lt;br /&gt;
             puts &amp;quot;I am a whinnying horse who can fly and chirp ! &amp;quot;&lt;br /&gt;
         end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  Aviary = Pegasus.new &lt;br /&gt;
  Aviary.talk         &lt;br /&gt;
&lt;br /&gt;
When the method talk is called on the Aviary object, Ruby makes sure that the latest method defined in the hierarchy is chosen. Here in this example, the talk method in the Pegasus class is defined the latest and hence “I am a whinnying horse who can fly and chirp !” is printed. If there was no method defined in the class, method from the Bird is printed since it is next in the hierarchy specified by the include statements. This way Ruby prevents any naming conflicts by always executing the most recently defined method.&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52952</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52952"/>
		<updated>2011-10-20T05:37:20Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Multiple Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect) this will, ‘behind the scenes’, call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Multiple Inheritance refers to a feature in some object oriented programming languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52950</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52950"/>
		<updated>2011-10-20T05:36:00Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Multiple Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect) this will, ‘behind the scenes’, call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Multiple Inheritance refers to a feature in some object oriented programming languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;br /&gt;
&lt;br /&gt;
Ruby does not support multiple inheritance. Mixin provide a way which eliminates the need for multiple inheritance. Ruby has modules which are just like abstract classes&amp;lt;ref&amp;gt;[http://download.oracle.com/javase/tutorial/java/IandI/abstract.html Abstract Classes]&amp;lt;/ref&amp;gt; in Java. Modules can have different methods implemented in them. They cannot be instantiated like abstract classes. They cannot inherit from other modules or classes. Ruby classes can inherit from only one superclass but can have unlimited number of modules in them to exploit the usage of predefined implementations in these modules. This provides functionality similar to multiple inheritance avoiding any ambiguities.&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52745</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52745"/>
		<updated>2011-10-19T22:56:08Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Comparable */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect) this will, ‘behind the scenes’, call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Multiple Inheritance refers to a feature in some object oriented programming languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52736</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52736"/>
		<updated>2011-10-19T21:14:30Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Composing modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin of Fixnum class to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect) this will, ‘behind the scenes’, call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Multiple Inheritance==&lt;br /&gt;
&lt;br /&gt;
Multiple Inheritance refers to a feature in some object oriented programming languages in which a class can inherit behaviors from more than one superclass. This contrasts with single inheritance in which a class can inherit behavior from at most one superclass. Though Multiple Inheritance provides its own advantages of improved modularity and ease of reuse of code in certain applications, it has its own set of disadvantages which sometimes outweigh the possible advantages.&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52726</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52726"/>
		<updated>2011-10-19T20:48:00Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Built in Modules : Comparable and Enumerable */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
= Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin of Fixnum class to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect) this will, ‘behind the scenes’, call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52725</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52725"/>
		<updated>2011-10-19T20:47:14Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Enumerable module */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
=Built in Modules : Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin of Fixnum class to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Composing modules==&lt;br /&gt;
Enumerable is a standard mixin, which can be included in any class. It is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect) this will, ‘behind the scenes’, call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52717</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52717"/>
		<updated>2011-10-19T20:24:04Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Enumerable module */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
=Built in Modules : Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin of Fixnum class to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Enumerable module==&lt;br /&gt;
&lt;br /&gt;
Enumerable is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The Enumerable module is included in the Collection class. We can hence initialize a Collection object with an array, which will be stored in the instance variable, @items. When we call one of the methods provided by the Enumerable module (such as min, max or collect) this will, ‘behind the scenes’, call the each method in order to obtain each piece of data one at a time.&lt;br /&gt;
  things = Collection.new(['x','yz','defgh','ij','klmno']) &lt;br /&gt;
   puts( things.min ) #=&amp;gt;  &amp;quot;defgh&amp;quot; &lt;br /&gt;
   puts( things.max ) #=&amp;gt;  &amp;quot;yz&amp;quot;&lt;br /&gt;
   puts( things.collect{ |i| i.upcase } ) #=&amp;gt; [&amp;quot;X&amp;quot;, &amp;quot;YZ&amp;quot;, &amp;quot;DEFGH&amp;quot;, &amp;quot;IJ&amp;quot;, &amp;quot;KLMNO&amp;quot;]&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52715</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52715"/>
		<updated>2011-10-19T20:22:57Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Enumerable module */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
=Built in Modules : Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin of Fixnum class to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Enumerable module==&lt;br /&gt;
&lt;br /&gt;
Enumerable is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
&lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from the example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as Arrays , Range , Hash etc which contains the Enumerable module.&lt;br /&gt;
&lt;br /&gt;
It is also possible to be able to apply Enumerable methods to classes which do not descend from existing classes which implement those methods. This can be done by including the Enumerable module in the class and then writing an iterator method called “each” .&lt;br /&gt;
Consider a class Collection, which does not have the enumerable method in its ancestors.&lt;br /&gt;
&lt;br /&gt;
  class Collection &lt;br /&gt;
   include Enumerable&lt;br /&gt;
   def initialize( someItems ) &lt;br /&gt;
   @items = someItems &lt;br /&gt;
   end &lt;br /&gt;
   def each &lt;br /&gt;
   @items.each{ |i| &lt;br /&gt;
   yield( i ) }&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52714</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52714"/>
		<updated>2011-10-19T20:13:20Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Enumerable module */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
=Built in Modules : Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin of Fixnum class to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Enumerable module==&lt;br /&gt;
&lt;br /&gt;
Enumerable is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from our example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example).&lt;br /&gt;
 &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as arrays , range , hash which contains the Enumerable module.&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52713</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52713"/>
		<updated>2011-10-19T20:12:53Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Enumerable module */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
=Built in Modules : Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin of Fixnum class to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Enumerable module==&lt;br /&gt;
&lt;br /&gt;
Enumerable is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;br /&gt;
&lt;br /&gt;
Consider the example of summing the elements of an array.&lt;br /&gt;
  [1, 2, 3, 4].inject  { |result, element| result + element }   # =&amp;gt; 10&lt;br /&gt;
&lt;br /&gt;
The inject method takes an argument and a block. The block will be executed once for each element contained in the object that inject was called on ([1,2,3,4] in our example). The argument passed to inject will be yielded as the first argument to the block, the first time it's executed. The second argument yielded to the block will be the first element of the object that we called inject on. If a default value is not passed in as an argument when the block executes for the first time, the first argument will be set to the first element of the enumerable and the second argument will be set to the second element of the enumerable. &lt;br /&gt;
Since there is no default value passed in as an argument,  the first time the block executes the first argument (result from the example) will be set to the first element of the array (1 from our example) and the second argument (element from the example) will be set to the second element of the enumerable (2 from the example). &lt;br /&gt;
The block will need to be executed 3 times, since the first execution will yield both the first and the second element. The first time the block executes it will add the result, 1, to the element, 2, and return a value of 3. The second time the block executes the result will be 3 and the element will also be 3 giving a return value of 6. The third and the final time the block executes, the result will be 6 and the element will be 4 , giving a return value of 10 which is the output. &lt;br /&gt;
&lt;br /&gt;
The inject method and the other methods as mentioned above can be directly applied to built in classes such as arrays , range , hash which contains the Enumerable module.&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52712</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52712"/>
		<updated>2011-10-19T20:00:01Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Comparable */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
=Built in Modules : Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin of Fixnum class to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Enumerable module==&lt;br /&gt;
&lt;br /&gt;
Enumerable is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method.  The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52711</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52711"/>
		<updated>2011-10-19T19:59:32Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Comparable */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
=Built in Modules : Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin of Fixnum class to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
==Enumerable module==&lt;br /&gt;
&lt;br /&gt;
Enumerable is a built in mix-in module for enumeration which provides collection classes with several traversal and searching methods, and with the ability to sort. &lt;br /&gt;
The Enumerable method is already included in the Array class and provides arrays with a number of useful methods such as include? which returns true if a specific value is found in an array, min which returns the smallest value, max which returns the largest and collect which creates a new array made up of values returned from a block.&lt;br /&gt;
&lt;br /&gt;
  arr = [1,2,3,4,5]&lt;br /&gt;
   y = arr.collect{ |i| i } #=&amp;gt; y = [1, 2, 3, 4]&lt;br /&gt;
   z = arr.collect{ |i| i * i } #=&amp;gt; z = [1, 4, 9, 16, 25] &lt;br /&gt;
   arr.include?( 3 ) #=&amp;gt; true &lt;br /&gt;
   arr.include?( 6 ) #=&amp;gt; false &lt;br /&gt;
   arr.min #=&amp;gt; 1 &lt;br /&gt;
   arr.max #=&amp;gt; 5 &lt;br /&gt;
&lt;br /&gt;
Another important method which is provided by the Enumerable module is the inject method. The inject method.  The inject method can be used to repeatedly apply an operation to adjacent elements in a collection.&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52709</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52709"/>
		<updated>2011-10-19T19:56:13Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Comparable */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
=Built in Modules : Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin of Fixnum class to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is the output printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52708</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52708"/>
		<updated>2011-10-19T19:55:46Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Comparable */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
=Built in Modules : Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;=&amp;gt; function uses Comparable mixin of Fixnum class to compare the area of two Squares. We can call the Comparable methods on Square objects.&lt;br /&gt;
  s1 = Square.new(3)&lt;br /&gt;
  s2 = Square.new(4)&lt;br /&gt;
&lt;br /&gt;
   if s1 &amp;lt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is smaller than Square 2&amp;quot;   # -&amp;gt; This is printed.&lt;br /&gt;
   else if s1 &amp;gt; s2&lt;br /&gt;
   puts &amp;quot;The area of Square 1 is larger than Square 2&amp;quot;&lt;br /&gt;
   else&lt;br /&gt;
   puts &amp;quot;The area of Square 1 equals to Square 2&amp;quot;    &lt;br /&gt;
   end&lt;br /&gt;
  end&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52707</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52707"/>
		<updated>2011-10-19T19:48:30Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Comparable */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
=Built in Modules : Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
   attr_reader :x&lt;br /&gt;
   def initialize(x)&lt;br /&gt;
   @x = x&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52706</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52706"/>
		<updated>2011-10-19T19:47:54Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Require / Load */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;br /&gt;
&lt;br /&gt;
=Built in Modules : Comparable and Enumerable=&lt;br /&gt;
&lt;br /&gt;
==Comparable==&lt;br /&gt;
&lt;br /&gt;
The Comparable is a built in mixin module that provides the neat ability to define one’s own comparison ‘operators’ such as &amp;lt;, &amp;lt;=, ==, &amp;gt;= by mixing the module into your class and defining the &amp;lt;=&amp;gt; method. It is then possible to specify the criteria for comparing some value from the current object with some other value. The  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 respectively. &lt;br /&gt;
&lt;br /&gt;
Assume we have a square class.&lt;br /&gt;
  &lt;br /&gt;
  class Square&lt;br /&gt;
    attr_reader :x&lt;br /&gt;
    def initialize(x)&lt;br /&gt;
	@x = x&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to compare the areas of two Squares , we first need to include the Comparable mixin.  &lt;br /&gt;
  class square&lt;br /&gt;
   include Comparable&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   .&lt;br /&gt;
   def area&lt;br /&gt;
   x*x&lt;br /&gt;
   end&lt;br /&gt;
   def &amp;lt;=&amp;gt;(other)&lt;br /&gt;
   self.area&amp;lt;=&amp;gt;other.area&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52705</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4c dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=52705"/>
		<updated>2011-10-19T19:41:34Z</updated>

		<summary type="html">&lt;p&gt;Dnyayac: /* Modules as Namespaces */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==4c Chapter 6==&lt;br /&gt;
=Regular Expressions=&lt;br /&gt;
&lt;br /&gt;
=Modules=&lt;br /&gt;
&lt;br /&gt;
Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit. This is useful so that one can avoid clashes with existing classes, methods, and constants, and also so that you can add (mix in) the functionality of modules into your classes. &lt;br /&gt;
The definition of a module is very similar to a class. Also, modules and classes are closely related. The Module class is the immediate ancestor of the Class class. Just like a class, a module can contain constants, methods and classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby programming language , a module is defined in the following way &lt;br /&gt;
  module &amp;lt;module name&amp;gt;&lt;br /&gt;
    statement1&lt;br /&gt;
    statement2&lt;br /&gt;
    ...........&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
Consider for example ,a module called MyModule , which defines the happy and the sad times. &lt;br /&gt;
&lt;br /&gt;
  module MyModule &lt;br /&gt;
   GOODMOOD = &amp;quot;happy&amp;quot;&lt;br /&gt;
   BADMOOD = &amp;quot;sad&amp;quot; &lt;br /&gt;
   def greet &lt;br /&gt;
   return &amp;quot;I'm #{GOODMOOD}. How are you?&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def MyModule.greet&lt;br /&gt;
   return &amp;quot;I'm #{BADMOOD}. How are you?&amp;quot; &lt;br /&gt;
   end&lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
The above represents a module MyModule with a constant GOODMOOD and an “instance method” greet.&lt;br /&gt;
In addition to instance methods a module may also have module methods. Just as class methods are prefixed with the name of the class, module methods are prefixed with the name of the module as shown above in MyModule.greet.&lt;br /&gt;
&lt;br /&gt;
In spite of their similarities, there are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.&lt;br /&gt;
Inspite of inability of the modules to be initialized and inherited., They provide a namespace and prevent name clashes and they implement the mixin facility.&lt;br /&gt;
&lt;br /&gt;
===Modules as Namespaces===&lt;br /&gt;
Modules can be considered as a named ‘wrapper’ around a set of methods, constants and classes. The various bits of code inside the module share the same ‘namespace’ so they are all visible to each other, but not visible to code outside the module. &lt;br /&gt;
&lt;br /&gt;
Consider the example of the MyModule described above&lt;br /&gt;
&lt;br /&gt;
We can access the module constants just as we would access class constants using the :: scope resolution operator like this: &lt;br /&gt;
  puts(MyModule::GOODMOOD)   #-&amp;gt;  happy&lt;br /&gt;
  puts(MyModule::BADMOOD)   #-&amp;gt;  sad&lt;br /&gt;
&lt;br /&gt;
We can access module methods using dot notation – that is, specifying the module name followed by a period and the method name&lt;br /&gt;
For example&lt;br /&gt;
  puts( MyModule.greet )     # -&amp;gt;  I’m sad. How are you?&lt;br /&gt;
Since modules define a closed space, we cannot access the instance method “greet” from outside the module.  &lt;br /&gt;
  puts greet   # -&amp;gt; NameError: undefined local variable or method `greet' for main:Object&lt;br /&gt;
&lt;br /&gt;
In case of classes , we could have created instances of the class which would all have access to the instance methods of the class. However modules cannot be instantiated.  This is where mixins come into picture.&lt;br /&gt;
&lt;br /&gt;
=Mixins=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A mixin is a class that is mixed with a module or a set of modules. In other words the implementation of the class and module are intertwined and combined. The real usage of a mixin is exploited when the code in the mixin starts to interact with code in the class that uses it.In order to mix the modules into the class we make use of the “'''include'''” method.  As it is included, the instance methods in the modules can be used just as though it were a normal instance method within the current scope.&lt;br /&gt;
&lt;br /&gt;
The process of including a module is also called ‘mixing in’ – which explains why included modules are often called ‘mixins’. When you mix modules into a class definition, any objects created from that class will be able to use the instance methods of the mixed-in module just as though they were defined in the class itself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class MyClass&lt;br /&gt;
   include MyModule &lt;br /&gt;
   def sayHi&lt;br /&gt;
   puts( greet )&lt;br /&gt;
   end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
Not only can the methods of this class access the greet method from MyModule, but so too can any objects created from the class:&lt;br /&gt;
&lt;br /&gt;
  ob = MyClass.new &lt;br /&gt;
  ob.sayHi     # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
  puts(ob.greet)   # -&amp;gt; I'm happy. How are you?&lt;br /&gt;
&lt;br /&gt;
=== Require / Load ===&lt;br /&gt;
&lt;br /&gt;
As programs get bigger and bigger, the amount of reusable code also increases. It would best to break this code into separate files, so that these files can be shared across different programs.  &lt;br /&gt;
Typically the code organizes these files as class or module libraries. In order to incorporate the reusable code into new programs Ruby provides two statements. &lt;br /&gt;
  load &amp;quot;&amp;lt;filename.rb&amp;gt;”&lt;br /&gt;
  require “&amp;lt;filename&amp;gt;”&lt;br /&gt;
&lt;br /&gt;
The load method includes the named Ruby source file every time the method is executed, whereas require loads any given file only once&lt;br /&gt;
&lt;br /&gt;
Consider the following ruby module written in the file Week.rb.&lt;br /&gt;
&lt;br /&gt;
  module Week&lt;br /&gt;
   FIRST_DAY = &amp;quot;Sunday&amp;quot;&lt;br /&gt;
   def Week.weeks_in_month&lt;br /&gt;
      puts &amp;quot;You have four weeks in a month&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
   def Week.weeks_in_year&lt;br /&gt;
      puts &amp;quot;You have 52 weeks in a year&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
In order to include the module Week into a class in another file, we need to load the Week.rb file first and then include it in the class.&lt;br /&gt;
&lt;br /&gt;
  class Decade&lt;br /&gt;
   require &amp;quot;Week&amp;quot;&lt;br /&gt;
   include Week&lt;br /&gt;
   no_of_yrs=10&lt;br /&gt;
   def no_of_months&lt;br /&gt;
      puts Week::FIRST_DAY&lt;br /&gt;
      number=10*12&lt;br /&gt;
      puts number&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
  d1=Decade.new&lt;br /&gt;
  puts Week::FIRST_DAY  # -&amp;gt; Sunday&lt;br /&gt;
  puts Week.weeks_in_month   #-&amp;gt; You have four weeks in a month&lt;br /&gt;
  puts Week.weeks_in_year    # -&amp;gt; You have 52 weeks in a year&lt;br /&gt;
  puts d1.no_of_months      # -&amp;gt; Sunday    120&lt;br /&gt;
&lt;br /&gt;
The important things to consider is:&lt;br /&gt;
&lt;br /&gt;
	'''include''' makes features available, but does not execute the code.&lt;br /&gt;
	&lt;br /&gt;
	'''require''' loads and executes the code one time (like a C #include).&lt;br /&gt;
&lt;br /&gt;
	'''load''' loads and executes the code every time it is encountered.&lt;br /&gt;
&lt;br /&gt;
However, in order to allow mixins we need to use include.&lt;/div&gt;</summary>
		<author><name>Dnyayac</name></author>
	</entry>
</feed>