What Is The Difference Between Modules And Classes In Ruby
Modules versus classes in ruby
Published:
Last modified:
Overview
A Module in Ruby is a collection of methods and constants.
Classes in Ruby are first-class objects, each is an instance of class
Class
.
Classes versus Modules
# | Class | Module |
---|---|---|
instantiation | can be instantiated | Can *not* be instantiated |
usage | object creation | Mixin facility. Provides a namespace. |
superclass | module | object |
methods | class methods and instance methods | module methods and instance methods |
inheritance | inherits behaviour and can be base for _inheritance_ | No _inheritance_ |
inclusion | cannot be included | can be included in classes and modules by using the include command (includes all instance methods as instance methods in a class/module) |
extension | can not extend with extend command (only with inheritance) | module can extend instance by using extend command (extends given instance with singleton methods from module) |
Table source: Difference between a class and a module
Modules are about providing methods that you can use across multiple classes - think about them as "libraries" (as you would see in a Rails app).
Classes are about objects; modules are about functions.
References
comments powered by Disqus
- Double Versus Single Quotes In RubyAugust 9, 2016
- Ruby Language OverviewAugust 9, 2016
- What Is The Difference Between Modules And Classes In Ruby
- Managing Ruby Gems Version For Each ProjectJune 10, 2016
Articles
Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.
Powered by SimpleIT Hugo Theme
·