<?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=Mssurani</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=Mssurani"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Mssurani"/>
	<updated>2026-09-30T12:15:41Z</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=53849</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=53849"/>
		<updated>2011-10-21T02:56:08Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: /* 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 discussed 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 discussed 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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53841</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=53841"/>
		<updated>2011-10-21T02:50:32Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: &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;
='''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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53840</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=53840"/>
		<updated>2011-10-21T02:50:15Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: &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;
='''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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53836</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=53836"/>
		<updated>2011-10-21T02:46:41Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: /* Disadvantages */&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/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;
='''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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53832</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=53832"/>
		<updated>2011-10-21T02:44:03Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: /* Advantages */&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/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;
='''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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53827</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=53827"/>
		<updated>2011-10-21T02:41:52Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: /* Multiple Inheritance */&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/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;
===Disadvantages===&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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53824</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=53824"/>
		<updated>2011-10-21T02:40:47Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: /* Mixins */&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/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;
='''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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53822</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=53822"/>
		<updated>2011-10-21T02:40:31Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: /* 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/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;
='''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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53820</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=53820"/>
		<updated>2011-10-21T02:39:31Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: /* Comparable and Enumerable */&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/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;
='''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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53817</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=53817"/>
		<updated>2011-10-21T02:38:32Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: /* Character Classes */&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/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;
='''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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53816</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=53816"/>
		<updated>2011-10-21T02:35:02Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: /* Repetition */&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/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;
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;
    &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;
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;
='''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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53804</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=53804"/>
		<updated>2011-10-21T02:30:00Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: /* Regular Expressions */&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/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;
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;
    &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;
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;
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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53802</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=53802"/>
		<updated>2011-10-21T02:29:14Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: /* Character Classes */&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/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]&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;
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;
    &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;
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;
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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53798</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=53798"/>
		<updated>2011-10-21T02:28:09Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: /* Regular Expressions */&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/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]&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;
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;
    &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;
The following table explains the the different types of character classes:&amp;lt;ref name=&amp;quot;rubyregex&amp;quot;&amp;gt;[http://doc.infosnel.nl/ruby_regular_expressions.html]&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;
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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53783</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=53783"/>
		<updated>2011-10-21T02:14:24Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: /* Regular Expressions */&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 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]&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;
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;
    &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;
The following table explains the the different types of character classes:&amp;lt;ref name=&amp;quot;rubyregex&amp;quot;&amp;gt;[http://doc.infosnel.nl/ruby_regular_expressions.html]&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;
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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53216</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=53216"/>
		<updated>2011-10-20T21:44:58Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: /* Regular Expressions */&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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53215</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=53215"/>
		<updated>2011-10-20T21:29:37Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Regular Expressions=&lt;br /&gt;
In computing, a [http://en.wikipedia.org/wiki/Regular_expression 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;
='''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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53214</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=53214"/>
		<updated>2011-10-20T21:08:48Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: &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;
='''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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53213</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=53213"/>
		<updated>2011-10-20T21:03:08Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: &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;
='''Extending Objects'''=&lt;br /&gt;
Ruby can act as a 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 prototype-based language. The Ruby dynamic nature 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;
The module TA has a method gets_paycheck which returns true. This class is for students who are TAs&lt;br /&gt;
&lt;br /&gt;
&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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53212</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=53212"/>
		<updated>2011-10-20T21:02:15Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: &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;
='''Extending Objects'''=&lt;br /&gt;
Ruby can act as a set-based language, using include augments a class definition, and hence adds functionality to all objects of a class.&lt;br /&gt;
But, it can also act as a prototype-based language. The Ruby dynamic nature 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;
The module TA has a method gets_paycheck which returns true. This class is for students who are TAs&lt;br /&gt;
&lt;br /&gt;
&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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53207</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=53207"/>
		<updated>2011-10-20T20:36:25Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: &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;
='''Extending Objects'''=&lt;br /&gt;
The Ruby dynamic nature 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;
&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 on_pay_roll?&lt;br /&gt;
            false&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
 &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.on_pay_roll?    #=&amp;gt; false&lt;br /&gt;
    deepak.on_pay_roll?   #=&amp;gt; false&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
    module TA&lt;br /&gt;
        attr_accessor :course   &lt;br /&gt;
        def on_pay_roll?&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.on_pay_roll?    #=&amp;gt; false&lt;br /&gt;
    deepak.on_pay_roll?   #=&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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53170</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=53170"/>
		<updated>2011-10-20T19:24:06Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: &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;
&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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4c_dm&amp;diff=53145</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=53145"/>
		<updated>2011-10-20T18:52:54Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: &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 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>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=52697</id>
		<title>CSC/ECE 517 Fall 2011</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=52697"/>
		<updated>2011-10-19T18:45:19Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Link title]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a cs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ri]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b tj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c cm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c sj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c ka]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d sr]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e vs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a sc]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e dm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e an]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e lm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g vn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f sv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g jn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h ps]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i zf]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g rn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i cl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h hs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d gs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b ns]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b jp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a av]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f jm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e ad]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e kt]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e gp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b qu]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2c bs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2c rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a ca]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b rv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2c ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f vh]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e ps]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3a oe]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3h rr]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3h ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 4b js]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 4b ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i sd]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d mt]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d ls]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d ch]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4c ap]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4h sv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4e cl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4a ga]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4f sl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i js]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4f ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4c dm]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*[[trial]]&lt;/div&gt;</summary>
		<author><name>Mssurani</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_dm&amp;diff=45617</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1e dm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1e_dm&amp;diff=45617"/>
		<updated>2011-09-01T00:29:27Z</updated>

		<summary type="html">&lt;p&gt;Mssurani: /* '''Introduction''' */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch1 1e dm&lt;br /&gt;
----&lt;br /&gt;
='''Introduction'''=&lt;br /&gt;
This wiki article describes the [http://en.wikipedia.org/wiki/Block_(programming) block structured] and [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented] languages by highlighting their advantages and limitations along with providing a comparison between them.&lt;br /&gt;
&lt;br /&gt;
='''Block Structured Languages'''=&lt;br /&gt;
[http://en.wikipedia.org/wiki/Block_(programming) Block structured languages] are a class of high-level languages in which a program is made up of blocks. A [http://en.wikipedia.org/wiki/Block_(programming) block] is a section of code which can be grouped together. Blocks consist of one or more declarations and statements. A block structured programming language uses and permits the creation of blocks and may also include nested blocks as components. The major function of using blocks is to enable the groups of statements within the block to be treated as if they were a single statement. &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Block_(programming) Block structured programming] is related to [http://en.wikipedia.org/wiki/Structured_programming structured programming], which is a technique for organizing and coding computer programs in which a hierarchy of modules is used, each having a single entry and a single exit point, and in which control is passed downward through the structure without unconditional branches to higher levels of the structure. A structured program is decomposed into a hierarchy of [http://en.wikipedia.org/wiki/Process_(computing) processes]. A process in this context is a body of code, typically a function or subroutine, that takes some input and manipulates it to produce an output. A process may be composed of other, more specialized processes, i.e., it may be a function that calls other functions. Structured Programming uses three types of [http://en.wikipedia.org/wiki/Control_flow control flow]: sequential, test, and iteration.&lt;br /&gt;
&lt;br /&gt;
Examples of block structured programming languages are [http://en.wikipedia.org/wiki/ALGOL ALGOL], [http://en.wikipedia.org/wiki/Fortran FORTAN], [http://en.wikipedia.org/wiki/Pascal_(programming_language) Pascal], [http://en.wikipedia.org/wiki/C_(programming_language) C], [http://en.wikipedia.org/wiki/Python_(programming_language) Python], [http://en.wikipedia.org/wiki/Lisp Lisp], [http://en.wikipedia.org/wiki/Ada_(programming_language) Ada], [http://en.wikipedia.org/wiki/PL_SQL PL/SQL].&lt;br /&gt;
&lt;br /&gt;
==Characteristics==&lt;br /&gt;
*Takes on the [http://en.wikipedia.org/wiki/Top-down_design top-to-bottom approach].&lt;br /&gt;
*Based around [http://en.wikipedia.org/wiki/Data_structures data structures] and [http://en.wikipedia.org/wiki/Subroutines subroutines].&lt;br /&gt;
*Uses single entry and exit block.&lt;br /&gt;
*The names of variables and other objects such as procedures which are declared in outer blocks are visible inside other inner blocks, unless they are shadowed by an object of the same name.&lt;br /&gt;
*Splits the tasks into modular forms. &lt;br /&gt;
&lt;br /&gt;
==Advantages==&lt;br /&gt;
*Splitting of tasks into blocks/modules makes the program simpler and easier to read with less lines and codes.&lt;br /&gt;
*Leave the solution coarse where the solution is not interesting.&lt;br /&gt;
*Can save an order of magnitude in memory.&lt;br /&gt;
*Can save an order of magnitude in run time.&lt;br /&gt;
*Numerical accuracy improves with more grid where gradients are strongest.&lt;br /&gt;
*Being block-structured allows Fortran to be used for array operations.&lt;br /&gt;
*Overhead involved in coarse-fine interactions can be [http://en.wikipedia.org/wiki/Amortized_analysis amortized] over larger array calculations.&lt;br /&gt;
*Numerical accuracy is improved if the coarse-fine interface is kept to a codimension smaller than the solution.&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
*Communication patterns between levels can be complex.&lt;br /&gt;
*Algorithm complexity can be substantial.&lt;br /&gt;
*No [http://en.wikipedia.org/wiki/Abstraction_(computer_science) data abstraction] or [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) information hiding] supported.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
='''Object Oriented Languages'''=&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented programming] is an approach to programming in which each data item with the operations used on it is designated as an object; the routines used to operate on the data item are called methods; and objects are grouped in a hierarchy of classes, with each class inheriting characteristics from the class above it.&lt;br /&gt;
&lt;br /&gt;
The four fundamentals of object oriented programming languages are [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism], [http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance], [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation] and [http://en.wikipedia.org/wiki/Abstraction_(computer_science) Abstraction].&lt;br /&gt;
&lt;br /&gt;
Examples of object oriented programming languages are C++, Java, Smalltalk, Ruby.&lt;br /&gt;
&lt;br /&gt;
==Concepts==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object_(computer_science) Objects:]&lt;br /&gt;
This is the basic unit of object oriented programming. That is both data and function that operate on data are bundled as a unit called as object.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Class_(computer_science) Classes:]&lt;br /&gt;
The concept of class is similar to the concept of structure in C. In other words classes are the data types on which objects are created. So while a class is created no memory is allocated only when an object is created memory gets allocated.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Inheritance_(computer_science) Inheritance:]&lt;br /&gt;
As the name suggests Inheritance is the process of forming a new class from an existing class that is from the existing class called as base class, new class is formed called as derived class. This is a very important concept of object oriented programming since this feature helps to reduce the code size.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Abstraction_(computer_science) Data Abstraction:]&lt;br /&gt;
By this feature of object oriented programming it is possible to represent the needed information in program without presenting the details. Also by the feature of data abstraction it is possible to create user defined data types and thus increase the power of programming language.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Data Encapsulation:]&lt;br /&gt;
Data Encapsulation is the process of combining data and functions into a single unit called class. By this method one cannot access the data directly. Data is accessible only through the functions present inside the class. Thus Data Encapsulation gave rise to the important concept of data hiding.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism:]&lt;br /&gt;
The ability to use an operator or function in different ways in other words giving different meaning or functions to the operators or functions is called polymorphism. Poly refers many. That is a single function or an operator functioning in many ways different upon the usage is called polymorphism.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Method_overloading Overloading:]&lt;br /&gt;
The concept of overloading is also a branch of polymorphism. When the exiting operator or function is made to operate on new data type it is said to be overloaded.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reusability Reusability:]&lt;br /&gt;
That is object oriented programming has the feature of allowing an existing class which is written and debugged to be used by other programmers and there by provides a great time saving and also code efficiency to the language. Also it is possible to a have the existing class and adds new features to the existing class as pet the programmer’s choice.&lt;br /&gt;
&lt;br /&gt;
Thus the object oriented programming features helps the program ad there by users of the application to achieve increased performance, it saves time of developing the application, give optimized code for the application, helps in gaining secured applications and there by helps in easier maintenance.&lt;br /&gt;
&lt;br /&gt;
==Advantages==&lt;br /&gt;
*Simplicity: software objects model real world objects, so the complexity is reduced and the program structure is very clear. &lt;br /&gt;
*Modularity: each object forms a separate entity whose internal workings are decoupled from other parts of the system. Hence helps to reduce large problems to smaller, more manageable problems.&lt;br /&gt;
*Modifiability: it is easy to make minor changes in the data representation or the procedures in an OO program. Changes inside a class do not affect any other part of a program, since the only public interface that the external world has to a class is through the use of methods. *Extensibility: adding new features or responding to changing operating environments can be solved by introducing a few new objects and modifying some existing ones. &lt;br /&gt;
*Maintainability: objects can be maintained separately, making locating and fixing problems easier. &lt;br /&gt;
*Re-usability: objects can be reused in different programs.&lt;br /&gt;
&lt;br /&gt;
==Limitations==&lt;br /&gt;
*Over generalization&lt;br /&gt;
*Artificial class relations&lt;br /&gt;
*Unnecessary complications&lt;br /&gt;
&lt;br /&gt;
='''Compare and Contrast'''=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
='''Block Structure in Object Oriented Programming'''=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
='''Conclusion'''=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
='''See Also'''=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
='''References'''=&lt;br /&gt;
# [https://commons.lbl.gov/display/~dtgraves@lbl.gov/Advantages+and+disadvantages+of+block-structured+AMR Advantages of block structured languages]&lt;br /&gt;
# [http://wiki.answers.com/Q/Advantages_and_disadvantages_of_an_object-oriented_language_in_organization Advantages of object oriented languages]&lt;br /&gt;
# [http://www.dba-oracle.com/t_object_oriented_approach.htm Advantages of object oriented languages]&lt;br /&gt;
# [http://pages.cs.wisc.edu/~fischer/cs538.s08/lectures/Lecture06.4up.pdf]&lt;br /&gt;
&lt;br /&gt;
='''External Links'''=&lt;br /&gt;
# [http://www.youtube.com/watch?v=Dw49WkGukMs&amp;amp;feature=feedwll&amp;amp;list=WL Video for Structured Programming]&lt;/div&gt;</summary>
		<author><name>Mssurani</name></author>
	</entry>
</feed>