Friday, March 19, 2010

Ruby: Classes vs. Module

"Ruby Modules are similar to classes in that they hold a collection of methods, constants, and other module and class definitions. Modules are defined much like classes are, but the module keyword is used in place of the class keyword. Unlike classes, you cannot create objects based on modules nor can you subclass them; instead, you specify that you want the functionality of a particular module to be added to the functionality of a class, or of a specific object. Modules stand alone; there is no "module hierarchy" of inheritance. Modules is a good place to collect all your constants in a central location."

Require
require and load take strings as their arguments
[require "sinatra"]
[load "bicycle.rb"]


Include
accpet any number of Module objects
[include Enumerable, Comparable]

Every class ia a module, the include method does not allow a class to be included within another class.

Can mix in more than one module in a class. However, a class cannot inherit from more than one class.
Class names tend to be nouns, while module names are often adjectives.

Source #1
Source #2

No comments:

Post a Comment