0

How is a module different from a class, and when should we use modules? Can we create instances of a module, or inherit from one?

sawa
  • 165,429
  • 45
  • 277
  • 381
vicky
  • 885
  • 1
  • 7
  • 23
  • Please find this link there is your answer. http://stackoverflow.com/questions/151505/difference-between-a-class-and-a-module – Mohammad Shahadat Hossain Mar 14 '16 at 02:25
  • Try the ruby language docs. If you still have questions after reading that, ask em! http://ruby-doc.com/docs/ProgrammingRuby/html/tut_modules.html – jrochkind Mar 14 '16 at 02:32
  • I removed the Ruby on Rails tag, as it generally has nothing to do with classes and modules. It's a web framework for Ruby; the Ruby language (not Rails) is where modules and classes are implemented. Other than models (which are kind of an edge case), Rails uses classes and modules in the same way as the underlying Ruby language. – Todd A. Jacobs Mar 14 '16 at 02:49
  • The biggest practical difference is that Modules can't be instantiated. You can think of a Module as a container for constants (including other Modules and Classes) and static functions, and a Class as a template for object instances. – Chris Heald Mar 14 '16 at 03:21

3 Answers3

1

Pragmatic Differences and Oversimplifications

Modules and classes share a lot of similarities in Ruby. The pragmatic difference is that modules are generally used to:

  1. Provide namespacing to avoid classname collisions. For example, the following code would create a Bar class in two separate modules, addressable as Foo::Bar and Baz::Bar:

    module Foo
      class Bar; end
    end
    
    module Baz
      class Bar; end
    end
    
  2. Enclose or wrap multiple classes in a single namespace. For example, to provide Foo::Bar and Foo::Baz:

    module Foo
      class Bar; end
      class Baz; end
    end
    
  3. Provide mixins. Since Ruby only uses single inheritance, you #include modules within a class, or #extend objects with them. This allows you great flexibility in how you compose classes, as well as increasing reusability. For example, to provide instances of both Baz and Quux with a #bar method:

    module Foo
      def bar; end
    end
    
    class Baz
      include Foo
    end
    
    class Quux
      include Foo
    end
    

There are certainly other differences, but in general you can think of modules as special types of classes. This is technically true because Module.class === Class, but don't get carried away with the oversimplification. It should, however, provide you with a pragmatic starting point.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • 1
    Your points 1 and 2 are common for modules and classes. It is not characteristic of modules. – sawa Mar 14 '16 at 04:50
0

There are a bunch of different questions here, and I agree with the others that this is something you really need to get from some basic Ruby programming sources. However, a potted set of answers to get you going:

How is a module different from a class,

A class is a thing that makes objects. Objects have both actions and state: that is, you can tell an instance of a class to do things (in methods), and it will store information about itself (in attributes).

In Ruby we use a module for a bunch of things, mostly:

  • to group classes together
  • to make functionality to add to classes (a mixin).
  • when we want a bunch of actions but don't need a class.

and when should we use modules?

You can't naturally use a module to create objects like a class, and it doesn't really hold state like a class. So if you want to have a bunch of objects that each have state and can do stuff, you need a class, not a module.

If don't need those things, you are probably looking at a module. But Ruby is a really accomodating language -- there is more than one way to do anything.

Can we create instances of a module, or inherit from one?

Nope. You need a class for that.

Andy Jones
  • 1,074
  • 1
  • 10
  • 21
-1

You want to characterize (i.e., give methods, variables, constants to) a set of objects by assuming relations between the set of objects and character assigners (i.e., classes/modules). In general, not just in computing, there are two modes (among others) of having such relation.

One mode is that the objects have one and only one character assigner chosen from a given group. Examples from other fields are:

  • radio button in GUI
  • major mode in emacs text editor
  • a person's name, parents, etc.

In Ruby's object oriented programming model, this is expressed as the set of object being an instance of a class. Every Ruby object is an instance of at least one class, and at most one class. It can be an instance of String class or Array class, but not both.

The other mode is that for each character assigner (possibly multiple), the objects either carries that character or not, and that is individually decided for each character assigner. Examples from other fields are:

  • check box in GUI
  • minor mode in emacs text editor
  • whether a person is an alumni of a school, an (ex-)employee of a company, etc.

In Ruby's object oriented programming model, this is expressed as the set of object inheriting a module. A Ruby object may or may not inherit Enumerable module, it may or may not inherit Comparable, and that is decided individually.

sawa
  • 165,429
  • 45
  • 277
  • 381